-
February 27th, 2013, 08:52 PM
#1
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 ;
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|