Got exception in thread "main" java.util.NoSuchElementException -


just started doing java programming. searched stackoverflow , saw various solutions error none of them solved issue in program. program stops @ "enddata". i'm sure simple fix can't seem figure out:

contents of student.dat file :

mary 50 60 70 80 shelly 34 56 90 100 john 32 54 66 88 alfred 21 100 88 75 enddata 

program output:

 name of student mary  his/her average score 65  name of student shelly  his/her average score 70  name of student john  his/her average score 60  name of student alfred  his/her average score 71  name of student enddata exception in thread "main" java.util.nosuchelementexception     @ java.util.scanner.throwfor(scanner.java:862)     @ java.util.scanner.next(scanner.java:1371)     @ statistics.main(statistics.java:32) 

my code:

     import java.util.*;         import java.io.*;         public class statistics {           public static void main(string[] args)throws ioexception {         scanner in = new scanner (new filereader("c:\\students.dat"));          string name;         int namecount = 0;         int avg = 0;         int spanish = 0;         int math = 0;         int french = 0;         int english = 0;         int highspanish = 0;         int highmath = 0;         int highfrench = 0;         int highenglish = 0;         int highavg = 0;         name = in.next();         while (name!= "enddata") {             namecount++;             system.out.printf (" name of student " + name + "\n");             name = in.next();             spanish = integer.parseint(name);             if (spanish > highspanish) {             highspanish = spanish;             }             name = in.next();             math = integer.parseint(name);             if (math > highmath) {             highmath = math;             }             name = in.next();             french = integer.parseint(name);             if (french > highfrench) {             highfrench = french;             }             name = in.next();             english = integer.parseint(name);             if (english > highenglish) {             highenglish = english;             }             avg = (spanish + math + french + english) /4;             if (avg > highavg) {             highavg = avg;             }             system.out.printf (" his/her average score " + avg + "\n");             name = in.next();             }             system.out.printf (" number of students in class " +         namecount);             system.out.printf (" highest student average " + highavg);             system.out.printf (" highest score spanish " + highspanish);             system.out.printf (" highest score math " + highmath);             system.out.printf (" highest score french " + highfrench);             system.out.printf (" highest score english " + highenglish);         }     } 

the main problem in program comparing strings using != operator incorrect type string, use .equals() compare strings have change :

 while (name!= "enddata")  

to following:

 while (!name.equals("enddata"))  

and better approach use in.hasnext() chec reached end of file instead of checking manually.

and nosuchelementexception thrown because of following statement in.next() referring next line of scanner while doesn't have next lines.

note: use in.nextint() read integer values scanner (spannish, math, ...).

and better approach have change while loop :

 while (in.hasnext()) {   name = in.next();   spanish = in.nextint();    // , on  } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -