java - How to create a multidimensional Array to take input from .txt file and store the strings and the numbers separate -
public class arraydirectory { public static void main(string args[]) throws filenotfoundexception { string file = ("lab4b2.txt"); scanner scan = new scanner(new filereader(file)); // initialises scanner read file file string[][] entries = new string[100][3]; // creates 2d array 100 rows , 3 columns. int = 0; while(scan.hasnextline()){ entries[i] = scan.nextline().split("\t"); i++; } //loops through file , splits on tab (int row = 0; row < entries.length; row++) { (int col = 0; col < entries[0].length; col++) { if(entries[row][col] != null){ system.out.print(entries[row][col] + " " ); } } if(entries[row][0] != null){ system.out.print("\n"); } } //prints contents of array not "null" } }
how make following code split string pieces , store them in multidimentional array? example:
text:
123 abc 456
789 def 101 112
array
[123] [abc] [456]
[789] [def] [101] [112]
the numbers text being converted numbers before stored in array. believe have use integer parsed.int(). not sure how implement it
with following corrections, end entries array splitted strings correctly.
public static void main(string args[]) throws filenotfoundexception { string file = ("c:\\array.txt"); scanner scan = new scanner(new filereader(file)); // initialises scanner read file file string[][] entries = new string[100][3]; // creates 2d array 100 rows , 3 columns. int = 0; while(scan.hasnextline()) { string [] splittedentries = new string[3]; splittedentries = scan.nextline().split(" "); for( int inx = 0; inx < splittedentries.length; ++inx ) { entries[i][inx] = splittedentries[inx]; } i++; } }
at moment, entries array this:
entries[0] = { 123, abc, 456 }; entries[1] = { 789, def, 101 };
so, can write own loops , process required.
Comments
Post a Comment