java - How to reset Scanner? -


i want read text file , put each line in string (array of strings). requires scanning file twice, 1 figure out how many line there , time create array of strings of size. throws error. , reset method doesn't seem work.

        filereader read = null;     try {         read = new filereader("modulesin.txt");         //scan through , make array of strings - each line         scanner scan = new scanner(read);         while(scan.hasnextline())         {             numofmods++;             scan.nextline();         }          scan.reset();          lines = new string[numofmods];          for(int = 0; < numofmods; i++)             lines[i] = scan.nextline(); 

this sinppet of code relevant.

skip using standard array... it's waste of time scan through file , scan through again. use arraylist instead has dynamic size , convert standard array afterwards.

 bufferedreader in = new bufferedreader(new filereader("path/of/text"));         string str;          list<string> list = new arraylist<string>();         while((str = in.readline()) != null){             list.add(str);         }          string[] stringarr = list.toarray(new string[0]); 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -