Reputation: 1
I have the following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
1 at test.Main.main(Main.java:28)
I am a beginner and I do no really understand where the thing gets stucked. I presume that somewhere in the for
.
Some help pls?
MAIN CLASS
public static ArrayList<Patron> patr_list= new ArrayList<Patron> ();
public static ArrayList<Angajat> angj_list= new ArrayList<Angajat> ();
public static ArrayList<Persoana> pers_list = new ArrayList<Persoana>();
public static void main(String []args) throws Exception
{
BufferedReader flux_in= new BufferedReader (new InputStreamReader (new FileInputStream("C:/Users/Administrator/workspace/test/test.txt")));
String linie;
while((linie= flux_in.readLine()) != null)
{
String [] s;
s= linie.split(";");
if(linie.contains("angajat"))
{
angj_list.add(new Angajat( s[0], Integer.parseInt(s[1]), Calendar.getInstance(), s[3], s[4] ));
}
else
if(linie.contains("patron"))
{
patr_list.add(new Patron(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]), Integer.parseInt(s[3])));
}
pers_list.addAll(patr_list);
pers_list.addAll(patr_list);
System.out.println(String.format("Nume", "Varsta", "Data_angajarii", "Loc de munca", "Functia"));
for( Angajat angj: angj_list)
{
System.out.println(angj);
}
System.out.println(String.format("Nume", "Varsta", "Nr de firme", "Nr de angajti"));
for( Patron patr: patr_list)
{
System.out.println(patr);
}
}
}
}
Persoana CLASS
public String nume;
public int varsta;
public Persoana (String nume, int varsta)
{
this.nume=nume;
this.varsta=varsta;
}
public String getNume() {
return nume;
}
public void setNume(String nume)
{
this.nume = nume;
}
public int getVarsta()
{
return varsta;
}
public void setVarsta(int varsta)
{
this.varsta = varsta;
}
}
Patron CLASS
public int nr_firme;
public int nr_angajati;
public Patron(String nume, int varsta, int nr_firme, int nr_angajati)
{
super(nume,varsta);
this.nr_firme=nr_firme;
this.nr_angajati=nr_angajati;
}
public int getNr_firme()
{
return nr_firme;
}
public void setNr_firme(int nr_firme)
{
this.nr_firme = nr_firme;
}
public int getNr_angajati()
{
return nr_angajati;
}
public void setNr_angajati(int nr_angajati)
{
this.nr_angajati = nr_angajati;
}
public String toString()
{
return "Patronul are" + nr_firme + "firme si" + nr_angajati+ "numar de angajti";
}
Angajat CLASS
public Calendar data_angajarii;
public String loc_munca;
public String functie;
public Angajat(String nume, int varsta, Calendar data_angajarii, String loc_munca, String functie)
{
super(nume, varsta);
this.data_angajarii=data_angajarii;
this.loc_munca=loc_munca;
this.functie=functie;
}
public Calendar getData_angajarii()
{
return data_angajarii;
}
public void setData_angajarii(Calendar data_angajarii)
{
this.data_angajarii = data_angajarii;
}
public String getLoc_munca()
{
return loc_munca;
}
public void setLoc_munca(String loc_munca)
{
this.loc_munca = loc_munca;
}
public String getFunctie()
{
return functie;
}
public void setFunctie(String functie)
{
this.functie = functie;
}
public String toString()
{
return "Angajatul a fost angajat in data de" + data_angajarii + "lucreaza la " + loc_munca+ " si are functia de " + functie;
}
Text file
angajat ionel 16 1991,8,4 timisoara director;
patron gigel 45 100 5000;
Upvotes: 0
Views: 164
Reputation: 209122
Here is your problem'
angajat ionel 16 1991,8,4 timisoara director;
patron gigel 45 100 5000;
You're splitting by ";"
. The ";"
is at the end of the line. So the s[0]
is the entire line and there is no s[1]
or any index after that.
Instead split by this "[\\s;]"
. That should work. This will split by spaces and semi-colons.
After you split. Do this
String name = s[0] + " " s[1]; // This will concatenate the first two indices
Examine this chart to see correct indices.
new Angajat( s[0], Integer.parseInt(s[1]), Calendar.getInstance(), s[3], s[4])
angajat ionel 16 1991,8,4 timisoara director
name s[2] s[3] s[4] s[5]
new Patron(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]), Integer.parseInt(s[3])
patron gigel 45 100 5000
name s[2] s[3] s[4]
Upvotes: 0
Reputation: 36339
You have not provided for the case that your input string does not contain a semicolon, and hence the string array returned by split does not have a second element.
The same extends to subsequent fields you access. For example, if you string has only 1 semicolon, there will be no 3rd element in the string array, and so on.
Here a short explanation of the message you are getting:
java.lang.ArrayIndexOutOfBoundsException:
1 at test.Main.main(Main.java:28)
This tells you the following:
Equipped with that knowledge, you could've found the error yourself. For, in that line, there is only one array access that uses 1 as index.
Upvotes: 3
Reputation: 96016
When you do s= linie.split(";");
you assume (a wrong assumption) that there will always be s[0]
, s[1]
, s[2]
, s[3]
and s[4]
. (Think about what happens when your input doesn't contain ;
?).
You should verify that s
have the length you expect.
Upvotes: 2