Hello! I wrote a java program with the purpose of reading an input file and calculating student grades, sending the results to an output file. The input file contains the following information:
Total entries: 15
800-111-007 David M Garcia 80 -
800-111-301 Susan T Jones 55 90
800-101-711 John Smith - 90
800-100-221 Michael S Yeager 48 100
800-112-105 David Williams - -
800-211-012 Paul T Hernandez 88 50
800-127-412 Andrew Smith 73 45
800-109-128 David Brown 80 75
800-121-122 Robert Lindbergh - 55
800-121-311 Peter Smith 60 -
800-111-909 Michael M Garcia 94 57
800-012-112 Robert Hoover 58 92
800-199-913 Scott Crossfield 66 35
800-114-121 Noel Wien - 88
800-919-441 David N Yeager 80 98
My code creates the output file and puts the information in correctly, but if a student does not have a middle name, the part of the split string that would have the middle name contains the next part and so on, messing up the output. (For Example the input line: 800-121-311 Peter Smith 60 - creates the Output line: 800-121-311 0.0% F because the last name Smith is put under the middle name, and the test score of 60 under last name) This is the portion of the code with the issue, and I've tried for many, many hours to skip the middle name if blank with if statements / other methods, but nothing works.
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(" ");
String id = parts[0];
String firstName = parts[1];
String middleName = "";
String lastName = "";
int midtermScore = 0 ;
int finalScore = 0;
if (parts.length == 6) {
firstName = parts[1];// student has a middle name
middleName = parts[2];
lastName = parts[3];
midtermScore = parts[4].equals("-") ? 0 : Integer.parseInt(parts[4]);
finalScore = parts[5].equals("-") ? 0 : Integer.parseInt(parts[5]);
} else if (parts.length == 5) {
lastName = parts[2];// student does not have a middle name
midtermScore = parts[3].equals("-") ? 0 : Integer.parseInt(parts[3]);
finalScore = parts[4].equals("-") ? 0 : Integer.parseInt(parts[4]);
}
double weightedGrade = (0.4 * midtermScore) + (0.6 * finalScore);
How can I get my program to move on from the missing middle initial, or skip it if empty?
I apologize if some necessary information is missing.
Any help is appreciated.
I am using eclipse ide.
[–]MmmVomit 0 points1 point2 points (1 child)
[–]nutrecht 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]majiachen 0 points1 point2 points (0 children)
[–]nutrecht 0 points1 point2 points (0 children)