java - Meaning of this error in Eclipse -
java - Meaning of this error in Eclipse -
this question has reply here:
java.util.nosuchelementexception: no line found 3 answersexception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(unknown source) @ lineio.main(lineio.java:39)
there no lines reddish out. i'll post code...
import java.io.file; import java.io.filenotfoundexception; import java.io.printwriter; import java.util.scanner; public class lineio { public static void main(string[] args)throws filenotfoundexception{ scanner console = new scanner(system.in); system.out.print("enter input file name: "); string inputfilename = console.next(); system.out.print("output file: "); string outputfilename = console.next(); file inputfile = new file(inputfilename); scanner in = new scanner(inputfile); printwriter out = new printwriter(outputfilename); int linenumber = 1; int linenumber2 = 10; int linenumber3 = 20; int linenumber4 = 30; int linenumber5 = 40; system.out.println("enter name 5 names:"); string person1 = console.next(); string person2 = console.next(); string person3 = console.next(); string person4 = console.next(); string person5 = console.next(); double sum = 0; int j = 0; while (j < 10) { string line = in.nextline(); int num = integer.parseint(line); sum = sum+num; } double average = sum / 10; system.out.println(person1 + " average score " + average); system.out.println(person2 + " average score " + linenumber2); system.out.println(person3 + " average score " + linenumber3); system.out.println(person4 + " average score " + linenumber4); system.out.println(person5 + " average score " + linenumber5); in.close(); out.close(); } }
why go on error? cannot understand error message , not understand why code not implement. have input.txt , output.txt files set up...
a quick search came post: java.util.nosuchelementexception: no line found
you must not have plenty lines in file.
in add-on making sure have plenty lines in file, add together check next line avoid error:
while (j < 10) { if (in.hasnextline()){ string line = in.nextline(); int num = integer.parseint(line); sum += num; //minor alter here } else{ system.out.println("not plenty lines!"); break; } j++; //increment j }
java eclipse
Comments
Post a Comment