java - Reading from a text file into an array - getting "nulls" -
java - Reading from a text file into an array - getting "nulls" -
i'm reading file , copying array. file has 5 lines of text, sentence each. output "array size 5" nil after that. if add together print line of array, gives me 5 nulls...
can help explain did wrong? thanks!
class="snippet-code-js lang-js prettyprint-override">public static int buildarray() throws exception { system.out.println("buildarray starting "); java.io.file textfile; // declares variable of type file textfile = new java.io.file ("textfile.txt"); //reserves memory scanner input = null; seek { input = new scanner(textfile); } grab (exception ex) { system.out.println("exception in method"); system.exit(0); } int arraysize = 0; while(input.hasnextline()) { arraysize = arraysize + 1; if (input.nextline() == null) break; } system.out.println("array size " + arraysize); // move lines array string[] linesinram = new string[arraysize];// reserve memory int count = 0; if (input.hasnextline()) { while(count < arraysize) { system.out.println("test"); linesinram[count] = input.nextline(); system.out.println(linesinram[count]); count = count + 1; } }
in code
int count = 0; if (input.hasnextline())
the above hasnextline
false have read way through file.
either reset scanner origin of file, or utilize dynamic list e.g. arraylist
add together elements to.
java arrays
Comments
Post a Comment