java - InputMismatchException when scanning file of integers -
when run code, inputmismatchexception (see comment). why happen? file want read contains list of integers separated spaces.
below main method.
public static void main (string[] args) throws exception { scanner in = new scanner(system.in); system.out.println("enter file name: "); string fname = in.nextline(); system.out.println("enter starting x coordinate: "); int x = in.nextint(); system.out.println("enter starting y coordinate: "); int y = in.nextint(); coordinate startposition = new coordinate(x, y); in.close(); watermesa wm = new watermesa(fname); scanner fs = new scanner(fname); grid_width = fs.nextint(); // exception occurs here grid_height = fs.nextint(); // , possibly here too? int = 0; (int r = 0; r < grid_width; r++) { (int c = 0; c < grid_height; c++) { = fs.nextint(); grid[r][c] = i; jpanel panel = new jpanel(); primary.add(panel); fs.close(); wm.canflowoffmap(startposition); } } }
.......................................................................................................................................................
you're scanning file name string, not content of file. change scanner fs = new scanner(fname);
scanner fs = new scanner(new file("/path/to/your/files/"+fname));
.