The below Java program has a error message when I run the program that states: E
ID: 3864318 • Letter: T
Question
The below Java program has a error message when I run the program that states: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Quarterback.main(Quarterback.java:36). This occurs on line: avg = Integer.parseInt(arr[5])/Integer.parseInt(arr[4]);. Can you please edit this program so that no error messages occur and runs perfectly.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
public class Quarterback {
public static void main(String[] args) throws FileNotFoundException, IOException {
int avg =0;
int totalTDs =0;
int completionAvg =0;
int playerCount =0;
int totalComp =0;
File fout = new File("quarterback_results.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
try (BufferedReader br = new BufferedReader(new FileReader("quarterback_stats.txt"))) {
String line;
int firstLine=0;
while ((line = br.readLine()) != null) {
if(firstLine==0){
String firstLineOut = line+" CMP. Rate";
bw.write(line);
bw.newLine();
firstLine++;
}
else{
String[] arr = line.split("\s+");
avg = Integer.parseInt(arr[5])/Integer.parseInt(arr[4]);
String outputData = line+" "+avg;
bw.write(line);
bw.newLine();
totalTDs = totalTDs + Integer.parseInt(arr[7]);
totalComp = totalComp + Integer.parseInt(arr[5]);
}
}
//Counting number of players
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("quarterback_stats.txt")));
lnr.skip(Long.MAX_VALUE);
playerCount = lnr.getLineNumber() -1;
lnr.close();
completionAvg = totalComp / playerCount ;
String totalTdsOut = "Total TDs:"+totalTDs;
bw.write(totalTdsOut);
bw.newLine();
String compAvgOut = "Completion average of all players:"+completionAvg;
bw.write(compAvgOut);
bw.newLine();
}
bw.close();
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
public class Quarterback {
public static void main(String[] args) throws FileNotFoundException, IOException {
int avg =0;
int totalTDs =0;
int completionAvg =0;
int playerCount =0;
int totalComp =0;
File fout = new File("quarterback_stats.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
try (BufferedReader br = new BufferedReader(new FileReader("quarterback_stats.txt"))) {
String line;
int firstLine=0;
while ((line = br.readLine()) != null) {
if(firstLine==0){
String firstLineOut = line+" CMP. Rate";
bw.write(line);
bw.newLine();
firstLine++;
}
else{
String[] arr = line.split("\s+");
avg = Integer.parseInt(arr[5])/Integer.parseInt(arr[4]);
String outputData = line+" "+avg;
bw.write(line);
bw.newLine();
totalTDs = totalTDs + Integer.parseInt(arr[7]);
totalComp = totalComp + Integer.parseInt(arr[5]);
}
}
//Counting number of players
LineNumberReader lnr = new LineNumberReader(new FileReader(new File("quarterback_stats.txt")));
lnr.skip(Long.MAX_VALUE);
playerCount = lnr.getLineNumber() -1;
lnr.close();
completionAvg = totalComp / playerCount ;
String totalTdsOut = "Total TDs:"+totalTDs;
bw.write(totalTdsOut);
bw.newLine();
String compAvgOut = "Completion average of all players:"+completionAvg;
bw.write(compAvgOut);
bw.newLine();
}
bw.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.