any help would be apreciated with arrays, exception in thread
I am getting the following error.....
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at NameSplitter.main(NameSplitter.java:23)
Code:
import java.io.*;
import java.util.*;
import java.lang.*;
public class NameSplitter {
public static void main(String[] args) throws Exception
{
try {
File f = new File("C:/Users/dstevens/Desktop/names.txt");
Scanner sc = new Scanner(f);
//aList<Person> people = new ArrayList<Person>();
ArrayList<Person> people = new ArrayList<Person>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = line.split(" ");
String fname = details[0];
String lname = details[1];
Person p = new Person(fname, lname);
// people.add(p);
}
for(Person p: people){
System.out.println(p.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Person{
private String fname;
private String lname;
public Person(String fname, String lname){
this.fname = fname;
this.setName(lname);
}
public String getfname() {
return fname;
}
public void setfname(String fname) {
this.fname = fname;
}
public void setName(String lname) {
this.lname = lname;
}
public String getName() {
return lname;
}
public String toString(){
return this.fname + "\t\t " + this.lname ;
}
}
Re: any help would be apreciated with arrays, exception in thread
The exception is thrown in this line:
Code:
String lname = details[1];
because you are assuming that details will have (at least) two elements. I guess you expect the string read from the scanner three lines above that one will always contain a space and then the split method will produce an arrary with at least two elements.
Re: any help would be apreciated with arrays, exception in thread
I am new to java , so I am not quite sure what I need to do with it. could you please explain a little more in depth? thnaks
Re: any help would be apreciated with arrays, exception in thread
You call the split method to divide the string in portions (words) separated by a space character, which will produce an array of strings. The number of elements in this array depends on the number of spaces in the original string.
In the subsequent lines you process this output, putting the first element of the array in one variable (fname) and then you try to get the second element into another variable (lname), but here is where the out of bound exception occurs. This means that the array doesn't have a second element, because the original string didn't have spaces in it. This could be caused by an empty line, for instance, or a line where the words are separated by tab or another character.
You need to check that the array has the expected number of elements before trying to access them (use the length field of the array), or you could detect this issue before even calling the split method (check if the line of text you are going to split contains a space). Alternatively (if the words are separated by other characters) use a different regexp in split.
Re: any help would be apreciated with arrays, exception in thread
It is often useful to identify which line has caused the problem and skip it:
Code:
int lineCounter = 0;
while(sc.hasNextLine()) {
lineCounter++;
String line = sc.nextLine();
if ( (null != line) && (line.length() > 0) && line.contains(" ") ) {
String[] details = line.split(" ");
String fname = details[0];
String lname = details[1];
Person p = new Person(fname, lname);
people.add(p);
}
else {
System.err.println("Rejected input line: " + lineCounter);
}
}