Question: import java.util.Scanner; import java.io.*; public class HW3 { public static void main(String[] args) throws IOException //added throws clause { //declare variables int id,rightanswer,wronganswer,total,omitted,grade; double pct;
import java.util.Scanner; import java.io.*;
public class HW3 { public static void main(String[] args) throws IOException //added throws clause { //declare variables int id,rightanswer,wronganswer,total,omitted,grade; double pct; int studentnumber = 0;
File myFile = new File("myinput.txt");
Scanner inputFile = new Scanner(myFile); PrintWriter outputFile = new PrintWriter("myoutput.txt"); id = inputFile.nextInt(); while (id != -1) { rightanswer= inputFile.nextInt(); wronganswer= inputFile.nextInt(); total=rightanswer+wronganswer; omitted=50-total; grade=2*rightanswer; pct=rightanswer/total; outputFile.println("id"+id+" "); outputFile.println(rightanswer+"right"+"\t\t"+wronganswer+"wrong"+" "); outputFile.println("total answered"+"\t"+total+" "); outputFile.println("number omitted is"+"\t"+omitted+" "); outputFile.println("grade is"+"\t"+grade+" "); if(rightanswer>wronganswer) { outputFile.println("more right than wrong"); } else { outputFile.println("more wrong than right"); } outputFile.printf("correct answer pct. is"+pct); if(omitted>10) { outputFile.println("10 or more omitted"); } else { outputFile.println("less than 10 omitted"); } studentnumber++; id = inputFile.nextInt(); } outputFile.flush(); System.out.println(" The program has completed"); inputFile.close(); outputFile.close(); } }
When i run it,it shows:
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at HW3.main(HW3.java:51) what is wrong?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
