java - Why is hasNextInt() returning false here? -
this first time scanning file in java. i'm trying read list of integers 1 text file, operate on them, , read them file. initial file looks this:
1234 9876 2345
i'm using while loop , reading hasnextint (after running program , checking initial text file, i'm sure values there), i'm getting 0 output new text file, assume hasnextint() returning false. sure, skipped scan , operated directly on each arraylist element using loop, , output written new file perfectly. here's code:
try { file infile = new file("input.txt"); file outfile = new file("output.txt"); filewriter inwriter = new filewriter(infile); filewriter outwriter = new filewriter(outfile); scanner inreader = new scanner(infile); numberarray.add(1234); //adding numbers array initialized numberarray.add(9876); numberarray.add(2345); system.out.println("the raw numbers are:"); (int n : numberarray) { inwriter.write(n + "\r\n"); system.out.println(n); } system.out.println("\nthe new numbers are:"); while (inreader.hasnextint()) { int newnumber = inreader.nextint(); int checkdigit = (newnumber / 7); int finalnumber = (newnumber % checkdigit); outwriter.write("" + newnumber + finalnumber + "\r\n"); system.out.println("" + newnumber + finalnumber); } inwriter.close(); outwriter.close(); inreader.close(); } catch (filenotfoundexception fnfe) { system.out.println("there problem infile file"); } catch (ioexception ioe) { system.out.println("there problem opening outfile file"); }
you have close inwriter prior reading in numbers file.
inwriter won't output text file until closed, reader isn't seeing because technically nothing there yet.
Comments
Post a Comment