I don't know why I got the
" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1" can anyone help me solve it?
And the input.txt was like the following format
"abc as, okc, 56 , 41 , 157"
package n;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;
public class Main1 {
private static final Scanner scan = new Scanner(System.in);
private static String[] player = new String[622];
private static String DATA = " ";
private static int COUNTS = 0;
public static void main(String[] args) throws IOException {
System.out.println(title);
driver();
}
public static void driver() throws IOException {
boolean q = false;
ScanInput();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name with .txt");
String filename = scan.nextLine();
while (!q) {
q = processDetail(scan, filename);
}
scan.close();
System.out.print("Hope you enjoy this program! /nHave a nice day!!");
}
private static boolean processDetail(Scanner scan, String filename) throws IOException {
boolean q = false;
System.out.println("Enter Yor Q; Y for playing; Q for quiting ");
String input = scan.nextLine().trim();
if (input.equalsIgnoreCase("y") ) {
System.out.println("Enter the player's full name:");
String fullname= scan.nextLine().trim();
String sss=fullname;
player=sss.split(" ");
double percent= ProcessInput(player[0]);
if (percent <= 0) {
System.out.println("Sorry! The player may not shot, or the player wasn't in the league\n");
return q;
} else {
COUNTS++; // increment the counts when data are found in the file
String result =
"The eFG% for "+ player[0] + "is " +percent+"in the team "+player[1] ;
DATA += (COUNTS + ". " + result + "\n");
System.out.println(result + " will be stored in " + filename + "\n");
myFileWriter(filename, DATA);
return q;
}
} else if (input.equalsIgnoreCase("q") ) {
q = true;
return q;
} else {
System.out.println("WARNNING: please enter a valid input\n");
return q;
}
}
private static double ProcessInput(String fullname) {
int FGM = -1;
int threePM = -1;
int FGA = -1;
for (int i = 0; i < player.length; i++) {
String[] data = player[i].split(",");
String player = data[0];
if (fullname.equalsIgnoreCase(player)) {
FGM = Integer.parseInt(data[2]);
threePM = Integer.parseInt(data[3]);
FGA = Integer.parseInt(data[4]);
}
}
if (FGM <= 0 || threePM <= 0 || FGA <= 0) {
return -1;
} else
return efgCalculator(FGM, threePM, FGA);
}
private static double efgCalculator(int FGM, int threePM, int FGA) {
double efg = 100 * ((FGM +(threePM) * 0.5) / FGA);
double efgPercent = Math.round(efg);
return efgPercent;
}
private static void myFileWriter(String filename, String result) throws IOException {
try {
String Greeting = "Hi, here are your searching history!!\n\n";
FileWriter fw = new FileWriter(filename);
PrintWriter pw = new PrintWriter(fw);
pw.print(Greeting);
pw.print(result);
pw.close();
} catch (IOException e) {
System.out.println("I/O error occurs!!!");
}
}
private static void ScanInput() throws FileNotFoundException {
try {
File file = new File("input.txt");
Scanner scan = new Scanner(file);
int index = 0;
while (scan.hasNextLine()) {
player[index] = scan.nextLine();
index++;
}
scan.close();
} catch (FileNotFoundException e1) {
System.out.println("File not found or have some typo! Checked again!");
}
}
}