Saturday, May 31, 2014

Adding a Phone object into a Student, Employee objects.


I am a bit new to java, so bare with me. And ill try to explain as best as i can .

I have to read in a file that that has Student, Employee, Faculty....such as name, last name, salary...etc


Example of a file input



Java Code:



Student, John, Doe, [email]johnDoe[At]yahoo.com[/email], 123,Wonderland Ave, 21323,TX
PhoneNumber,Cell,111,222,3333
Student, Jesus,Satan,[email]jesus[At]satan.com[/email,666, HeavenHell Dr., 666666,CA
PhoneNumber,Work,111,333,5555


Java Code:



while(input.hasNext()){

String [] tokens = input.next().split(",");

String objectType = tokens[0];



if (objectType.equalsIgnoreCase("Person")){
String firstName = tokens[1];
String lastName = tokens[2];
Address address = new Address (Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]), tokens[5], tokens[6], tokens[7], Integer.parseInt(tokens[8]));
PhoneNumber phoneNumber = new PhoneNumber(tokens[1],Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]),Integer.parseInt(tokens[3]));
String emailAddress = tokens[9];

Person p = new Person(firstName, lastName, emailAddress, address,phoneNumber);

person.add(p);
}

else if (objectType.equalsIgnoreCase("Phone Number")){

String type = tokens[1];
int areaCode = Integer.parseInt(tokens[2]);
int prefix = Integer.parseInt(tokens[3]);
int suffix = Integer.parseInt(tokens[4]);

PhoneNumber ph = new PhoneNumber(type,areaCode,prefix,suffix);

phoneList.add(ph);

}

else if (objectType.equalsIgnoreCase("Student")){

String firstName = tokens[1];
String lastName = tokens[2];

Address address = new Address (Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]),tokens[5], tokens[6], tokens[7], Integer.parseInt(tokens[8]));
String emailAddress = tokens[9];
String classStanding = tokens[10];

PhoneNumber phoneNumber = new PhoneNumber(tokens[1],Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]),Integer.parseInt(tokens[4]));



Student s = new Student(firstName, lastName,emailAddress,address,phoneNumber, classStanding);
person.add(s);

}

There is more code, but it's repetitive. Now, I keep getting an error. Array of bounds. I believe i get the error because the phone number. The phone number is on the next line

I created a different arraylist for phonenumber, but I dont know how to match it with the correct person, student, employee...etc. Any help?

No comments:

Post a Comment