java - Every time i compile my project everything outside the while loop doesn't show up (input comes from file) -
import java.io.*; import java.util.*; public class statistics { public static void main(string[] args) throws ioexception { scanner in = new scanner(new filereader("c:\\users\\user\\desktop\\students.dat"));//opened file int math=0,spanish=0,french=0,english=0,count=0,sum,highspan=0,highmath=0,highfren=0,higheng=0;//declarations double avg,highavg=0; string name,highname; highname=""; name = ""; //prime read while (name!="enddata"){ //sentinel condition count++; //keeps count of students name = in.next(); spanish=in.nextint(); math=in.nextint(); french=in.nextint(); english=in.nextint(); sum = spanish + math + french + english; avg = sum/4; //claculates average system.out.printf("%s - %.0f\n",name,avg); if (avg > highavg){ //checks highest average highavg = avg; highname = name; } if (spanish > highspan){ highspan = spanish; } if (math > highmath){ highmath = math; //checks highest mark each subject } if (french > highfren){ highfren = french; } if (english > higheng){ higheng = english; } name = in.nextline(); } system.out.printf("number of students in class is: %d\n",count); system.out.printf("the highest student average is: %f\n",highavg); system.out.printf("the student made highest average is: %s\n",highname); system.out.printf("the highest spanish mark is: %d\n",highspan); system.out.printf("the highest math mark is: %d\n",highmath); system.out.printf("the highest french mark is: %d\n",highfren); system.out.printf("the highest english mark is: %d\n",higheng); in.close(); //closing of file } }
write java program read , process each line of textfile , print following information (for each line except last): number of students in class name , average score each student highest student average in class (ignore possibility of tie) name of student attained highest average (ignore possibility of tie) highest mark each subject
its supposed read data form dat file , make necessary output print statements outside while loop not showing output error below.
mary - 65 shelly - 70 john - 60 alfred - 71 exception in thread "main" java.util.nosuchelementexception @ java.util.scanner.throwfor(scanner.java:862) @ java.util.scanner.next(scanner.java:1485) @ java.util.scanner.nextint(scanner.java:2117) @ java.util.scanner.nextint(scanner.java:2076) @ statistics.main(statistics.java:17)
in.nextint() throwing nosuchelementexception.
your file seems have improper data.
your while loop never break because condition:
while(name!="enddata")
will true. that's why when token exhausted scanner object throws nosuchelementexception. , because of exception print statement after while loops not executed.
try use hasnext() method of scanner check next token.
replace while loop while.
while (in.hasnext()){ //sentinel condition count++; //keeps count of students name = in.next(); if(name.equals("enddata")){ break; } spanish=in.nextint(); math=in.nextint(); french=in.nextint(); english=in.nextint(); sum = spanish + math + french + english; avg = sum/4; //claculates average system.out.printf("%s - %.0f\n",name,avg); if (avg > highavg){ //checks highest average highavg = avg; highname = name; } if (spanish > highspan){ highspan = spanish; } if (math > highmath){ highmath = math; //checks highest mark each subject } if (french > highfren){ highfren = french; } if (english > higheng){ higheng = english; } }
students.dat file format should this:
shelly 2 2 3 4 brown 34 2 1 4 pink 23 45 21 1 enddata
Comments
Post a Comment