I\'m new to java and trying to create a program for an asssignment. It compiles,
ID: 3567721 • Letter: I
Question
I'm new to java and trying to create a program for an asssignment. It compiles, but I keep getting an exception in thread error. here is the program:
import java.util.Scanner;
import java.io.*;
public class grades {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
// Define file names
final String INPUT_FILE = "grades.txt";
final String OUTPUT_FILE = "grades.txt";
int grade;
String name = " ";
double value = 0;
String msg = " ";
String line = " ";
String filename = "ShelbyHarms_3_08_Input.txt";
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
System.out.println("Reading file " + INPUT_FILE + " " +
"Creating file " + OUTPUT_FILE);
// Read all of the values from the file
while (inputFile.hasNext() ) {
grade = inputFile.nextInt();
name = inputFile.nextLine();
name = name.trim();
} // End while
if(value >= 90)
{
msg = "OUTSTANDING";
}
else if (value >= 70)
{
msg = "Satisfactory";
}
if(value >= 90){
msg = "OUTSTANDING";
}else{
if(value >= 70){
msg = "Satisfactory";
}else
msg = "FAILING";
}
outputFile.println(value + " " + name + " " + msg);
outputFile.println("processed names");
outputFile.println("between 70 and 89 inclusive");
outputFile.close();
} // End outputResults
} // End class
Here is the error:
Reading file grades.txt
Creating file grades.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at grades.main(grades.java:42)
The Input file is this:
Data to use in this order:
------------------------------------
70 Light Karen L
99 Fagan Bert Todd
60 Antrim Forrest N
73 Camden Warren
80 Mulicka Al B
99 Lee Phoebe
75 Bright Harry
92 Garris Ted
83 Benson Martyne
71 Lloyd Jeanine D
43 Leslie Bennie A
40 Brandt Leslie
100 Schulman David
51 Worthington Dan
60 Hall Gus W
60 Prigeon Dale R
96 Fitzgibbons Rusty
Explanation / Answer
As I said .... the grade and name are in the same line.
So you need to read the line and split it when tab occurs, here's the correct while loop
Also this just removes you error....but i think you code is wrong somewhere....post the exact question if you want hlp with it. Hope it helps!
while (inputFile.hasNext() ) {
String lines=inputFile.nextLine();
String[] words=lines.split(" ");
grade=Integer.parseInt(words[0]);
name=words[1];
name = name.trim();
} // End while
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.