I can't seem to get Java to read my text document -
import java.util.scanner; public class average { public void average() { scanner in = (new scanner("j:\\ap comptuter science\\semester 2\\exeptions\\13.1\\numbers.txt")); try{ string test = in.nextline(); } catch(nullpointerexception i) { system.out.println("error: " + i.getmessage()); } int total = 0; int counter = 0; while(in.hasnextint()){ total = total + in.nextint(); counter++; } total = total / counter; system.out.println(total); } }
i have project ap comp class , did work according notes, file "numbers" isn't being read , answer 0 when should huge number.
firstly should correct path, , put in same directory class files. , instead of providing path scanner should give file. should this.
import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class average { public void printaverage(){ file file = new file(""j:\\ap comptuter science\\semester 2\\exeptions\\13.1\\numbers.txt""); scanner scan; try { scan = new scanner(file); int total = 0, counter = 0; while(scan.hasnextint()){ system.out.println("loop"); total = total + scan.nextint(); counter++; } if(counter != 0) total = total/counter; system.out.println(total); } catch (filenotfoundexception e) { e.printstacktrace(); } } }
Comments
Post a Comment