I have a program that is supposed to read from a txt file, compute the necessary functions, then print to another file. I can't seem to find the inputmismatchexception. Any help would be greatly appreciated.
I tried casting, when I realized it might be the input file causing it, I corrected the order. Am I missing something?
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// Create an ArrayList to store the students
ArrayList<Student> studentList = new ArrayList<>();
// Read data from input file and create Student objects
try {
Scanner input = new Scanner(new File("C:\\Users\\BP\\IdeaProjects\\Input.txt"));
while (input.hasNext()) {
String SID = input.next();
int S1 = input.nextInt();
int S2 = input.nextInt();
int S3 = input.nextInt();
String name = input.next();
int totalHr = input.nextInt();
double GPA = input.nextDouble();
Student student = new Student(SID, name, S1, S2, S3, totalHr, GPA);
studentList.add(student);
}
input.close();
} catch (FileNotFoundException e) {
System.err.println("Input file not found.");
System.exit(1);
}
// Calculate the percentage score and assign letter grades for each student
for (Student student : studentList) {
student.calculatePercentageScore();
student.calculateLetterGrade();
}
// Print student information including % score and grades
for (Student student : studentList) {
student.printStudentInfo();
}
// Delete students with specified IDs
deleteStudent(studentList, "42P4");
deleteStudent(studentList, "45A3");
// List the ArrayList after deleting students
System.out.println("\nAfter deleting students:");
for (Student student : studentList) {
student.printStudentInfo();
}
// Add new students to the ArrayList
Student student1 = new Student("67T4", "Clouse B",80, 75, 98, 102, 3.65);
Student student2 = new Student("45P5", "Garrison J",75, 78, 72, 39, 1.85);
Student student3 = new Student("89P0", "Singer A",85, 95, 99, 130, 3.87);
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
// Calculate grades and sort the ArrayList by % score (lower to higher)
bubbleSort(studentList);
// Print the sorted ArrayList
System.out.println("\nAfter sorting by % score (lower to higher):");
for (Student student : studentList) {
student.printStudentInfo();
}
}
// Function to delete a student from the ArrayList by StudentID
public static void deleteStudent(ArrayList<Student> studentList, String studentID) {
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getStudentID().equals(studentID)) {
studentList.remove(i);
System.out.println("Student with ID " + studentID + " has been deleted.");
break; // No need to continue searching
}
}
}
// Function to sort the ArrayList by % score (lower to higher)
public static void bubbleSort(ArrayList<Student> studentList) {
int n = studentList.size();
boolean swapped;
do {
swapped = false;
for (int i = 0; i < n - 1; i++) {
if (studentList.get(i).getPercentageScore() < studentList.get(i + 1).getPercentageScore()) {
// Swap the students
Student temp = studentList.get(i);
studentList.set(i, studentList.get(i + 1));
studentList.set(i + 1, temp);
swapped = true;
}
}
} while (swapped);
}
}
class Student {
private String studentID;
private int test1, test2, test3;
private String name;
private int percentageScore;
private char letterGrade;
private int totalHr;
private double GPA;
public Student( String studentID, String name,int test1, int test2, int test3, int totalHour, double GPA) {
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
this.studentID = studentID;
this.name = name;
this.totalHr=totalHr;
this.GPA=GPA;
}
public void calculatePercentageScore() {
percentageScore = (int)((test1 + test2 + test3) / 3.0);
}
public void calclateClass() {
if(totalHr >= 1 && totalHr <=30) {
}
}
public void calculateLetterGrade() {
if (percentageScore >= 90) {
letterGrade = 'A';
} else if (percentageScore >= 80) {
letterGrade = 'B';
} else if (percentageScore >= 70) {
letterGrade = 'C';
} else if (percentageScore >= 60) {
letterGrade = 'D';
} else {
letterGrade = 'F';
}
}
public void printStudentInfo() {
System.out.println("Student ID: " + studentID);
System.out.println("Name: " + name);
System.out.println("Test 1: " + test1 + ", Test 2: " + test2 + ", Test 3: " + test3);
System.out.println("Percentage Score: " + percentageScore);
System.out.println("Letter Grade: " + letterGrade);
System.out.println("Total hours: " +totalHr);
System.out.println("GPA: " + GPA);
}
public String getStudentID() {
return studentID;
}
public int getPercentageScore() {
return percentageScore;
}
}
/* Input File:
Student ID Student Name Test 1 Test 2 Test 3 Total Hr GPA
45A3 Jones_H_A 86 88 95 98 3.42
34K5 Horner_M 67 75 23 17 1.95
56J8 Gach_T_A 75 85 90 60 3.75
34U8 Hunter_C 100 50 75 75 2.60
42P4 Hinrichs_S 85 75 65 52 3.29
78T6 Johnson_K 80 78 89 15 2.00
44L2 Levitte_H 56 66 99 100 2.35
88I9 Garner_J 95 92 98 110 3.89
*/