I just woke up, and I know there's better ways of doing this, but can anyone help me with this code?
I just don't know how to fix the output where it skews the order of name/id/etc
Code
import java.util.Scanner;
import java.io.*;
public class Arrays_and_File_Operation
{
public static void main(String[] args) throws IOException
{
File file = new File("Student Data.txt");
Scanner keyboard = new Scanner(file);
int[] scores = new int[4];
String[] names = new String[5];
String[] iD = new String [5];
int[] average = new int[5];
String[] grade = new String[5];
int total = 0;
int classTotal = 0;
int classAverage = 0;
while(keyboard.hasNext())
{
for(int i=0;i<5;i++)
{
names[i] = keyboard.nextLine();
iD[i] = keyboard.nextLine();
for(int j=0;j<4;j++)
{
scores[j] = Integer.parseInt(keyboard.next());
total += scores[j];
}
average[i] = total / 4;
if (average[i] >= 90)
{
grade[i] = "A";
}
else if (average[i] >= 80)
{
grade[i] = "B";
}
else if (average[i] >= 70)
{
grade[i] = "C";
}
else
{
grade[i] = "F";
}
System.out.println("Name: " + names[i]);
System.out.println("ID: " + iD[i]);
System.out.print("Scores: ");
for(int j=0;j<4;j++)
{
System.out.print(scores[j] + " ");
}
System.out.println("\nAverage: " + average[i]);
System.out.println("Grade: " + grade[i]);
}
}
for(int i=0;i<5;i++)
{
classTotal += average[i];
classAverage = classTotal / 5;
}
System.out.println("Class Average: " + classAverage);
}
}
Output:
Code
Name: Amy Adams
ID: 10111
Scores: 97 86 78 95
Average: 89
Grade: B
Name:
ID: Ben Barr
Scores: 20222 89 81 73
Average: 5205
Grade: A
Name: 87
ID: Carla Carr
Scores: 30333 79 71 63
Average: 12841
Grade: A
Name: 77
ID: Don Davis
Scores: 40444 69 62 58
Average: 23000
Grade: A
Name: 67
ID: Edna Eaton
Scores: 50555 63 51 62
Average: 35682
Grade: A
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at Arrays_and_File_Operation.main(Arrays_and_File_Operation.java:26)
Input File:
Code
Amy Adams
10111
97 86 78 95
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48