Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at Test.main(Test.java:20)
What I have tried:
String path1 = " C:\\Users\\savac\\OneDrive\\Desktop\\Proiect\\RStudenti.csv" ; String line1 = " " ; Student[] studenti=new Student[ 2 ]; try { int i=0; BufferedReader br = new BufferedReader( new FileReader(path1)); while ((line1 = br.readLine())!=null) { String [] values = line1.split( " ," ); studenti[i]=new Student(values[ 0 ],values[ 1 ],Integer.parseInt(values[ 2 ])); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); studenti[i]= new Student(values[0],values[1],Integer.parseInt(values[2]));
Suspecting, mostly this line. You must have passed only 2 values comman separated as input leading to an error when you tried to access values[2]
Couple of suggestions:
1. Make sure to check for length before accessing the index
2. Use IDE Debugger and it would be easy to figure whats wrong. You will learn more when you do.
Quote:
ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2

Very simple !
You try to use the third value of an array which contain only 2 values.
You need to check the size of the array.
Do something like:
while ((line1 = br.readLine())!=null) { String [] values = line1.split( " ," ); if (size of values >= 3 ) { studenti[i]=new Student(values[ 0 ],values[ 1 ],Integer.parseInt(values[ 2 ])); } else { // report wrong contain of 'values' // print 'line1' and 'i' I let you search for exact syntax.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •