Java MisMatchException while using scanner, but why? -
hi trying read input.txt file using scanner, keep getting input mismatch exception , unsure why. file reading in formatted this: first single number identify array size. next line list of integers delimited commas. have fails on first integer being read in:
file inputfile = new file("input.txt"); scanner scan = new scanner(inputfile); int arraysize = scan.nextint(); scan.nextline(); int[] array = new int[arraysize]; (int = 0; < arraysize; i++) { array[i] = scan.nextint(); }
i think need in there catch commas after each int. maybe scan.next(",")? failing before first comma.
thanks in advance!
edit: input file example:
5 -1, -2, -3 , -4, -5
file inputfile = new file("c:\\file.txt"); scanner scan = new scanner(inputfile); string size = scan.nextline(); // read size string aux = scan.nextline(); aux = aux.replaceall("\\s",""); // remove whitespaces better integer.parseint(string) string[] parts = aux.split(","); system.out.println("size "+size); (int = 0; < parts.length; i++) { system.out.println(parts[i]); } scan.close();
then can convert strings integers.
Comments
Post a Comment