Getting an index out of bounds error, Code reads in name and grades from file and outputs file with name and averages Java
I wrote this code to take an input file:
The data has a name followed by their 3 test grades
"sally,smith" 50.6 26.7 90.3
"jane,Jones" 88.8 89.3 92.3
"bob,Frank" 67.3 70.5 80.3
"JOE,RETT" 100.0 0.0 75.5
"moe,MiLLer" 95.2 98.4 90.2
"anne,Abrams" 15.6 90.23 87.5
and return an output file in the form:
Student, Test Average
S.Smith,55.87
My code is below and im getting an index out of bounds error. Can someone explain why im getting this error? Thanks!
import java.util.*;
import java.io.*;
public class Q4
{
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(new File("Q4.txt"));
PrintWriter output = new PrintWriter(new File("Output.txt"));
double sum;
double average;
String parsed_name= "", first_name , last_name;
String[] splitted_string = new String[100];
String[] name = new String[100];
output.print("Student"+","+"Test Average\n");
while (in.hasNextLine()){
String line = in.nextLine();
sum=0.0;
parsed_name = "";
//split the line based on space
splitted_string = line.split(" ");
name=splitted_string[0].split(",");
first_name = name[0];
last_name = name[1];
parsed_name = parsed_name+Character.toString(Character.toUpperCase(first_name.charAt(0)));
last_name = last_name.toLowerCase();
last_name = last_name.substring(0, 1).toUpperCase() + last_name.substring(1);
sum = sum + Double.parseDouble(splitted_string[1]);
sum = sum + Double.parseDouble(splitted_string[2]);
sum = sum + Double.parseDouble(splitted_string[3]);
average = sum / 3;
//writing all the values to output file
output.print(parsed_name + "." +l ast_name+",");
output.printf("%.2f%n" + average);
}
//closing the files
output.close();
in.close();
}
}
from Recent Questions - Stack Overflow https://ift.tt/2V6u6ok
https://ift.tt/eA8V8J
Comments
Post a Comment