I need to read data from file in the format: int, Name nameClass, String, String, String, Scores [int,int,int,int,int], for example
150, John, Smith, USA, Active, Pro, 3,4,3,4,4
After running the code I get the following error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at f1_final.CompetitorList.processLine(CompetitorList.java:184)
at f1_final.CompetitorList.readFile(CompetitorList.java:149)
at f1_final.FileWriterComp.main(FileWriterComp.java:14)
I figured out that this is due to me trying to copy int to a String. How can i go around and get this fixed?
private void processLine(String line) {
String compNo = "";
try {
String [] parts = line.split(",");
nameClass compName = new nameClass(parts[1], parts[2]);
compNo = parts[0];
compNo = compNo.trim();
String compCountry = parts[3];
String compLevel = parts [4];
String compActive = parts[5];
int compNumber = Integer.parseInt(compNo);
int compScoreLength = parts.length-6;
int cScore[] = new int[compScoreLength];
System.arraycopy(parts, 6, cScore, 0, compScoreLength); // this line is causing problems which I wasnt able to fix. The program is unable to read arrays from file.
// int[]cScore = new int [compScoreLength];
// for(int i=0 ; i< compScoreLength ; i++)
// cScore[i] = parts[6].charAt(i)-'0';
Competitor c = new Competitor(compNumber, compName, compCountry, compLevel, compActive, cScore);
this.addOneCompetitor(c);
}