Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By kneitzel

Thread: Adding a Phone object into a Student, Employee objects.

  1. #1
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Adding a Phone object into a Student, Employee objects.

    I keep getting an error.
    If you need help with the error, copy the full text of the message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default Re: Adding a Phone object into a Student, Employee objects.

    Java Code:
    public class DataReader {
    	protected File file;
    
    	public DataReader(File file){
    		this.file = file;
    	}
    
    	public ArrayList<Person> readData() throws FileNotFoundException {
    
    		Scanner input = new Scanner(file);
    		ArrayList<Person> person= new ArrayList<Person>();
    		
    
    		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,null);
    
    				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);
    
    			}
    			else if (objectType.equalsIgnoreCase("Faculty:")){
    
    				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 phone = new PhoneNumber(tokens[1],Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]),Integer.parseInt(tokens[4]));
    				String emailAddress = tokens[9];
    				String officeLocation = tokens[10];
    				int salary = Integer.parseInt(tokens[11]);
    				String officeHours = tokens[12];
    				String rank = tokens[13];
    
    
    				Faculty fac = new Faculty(firstName, lastName, emailAddress,address,p,officeLocation,salary,officeHours,rank);
    				person.add(fac);
    			}	
    
    			else if (objectType.equalsIgnoreCase("Staff")){
    
    				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[2]), Integer.parseInt(tokens[3]),Integer.parseInt(tokens[4]));
    				String emailAddress = tokens[9];
    				String officeLocation = tokens[10];
    				int salary = Integer.parseInt(tokens[11]);
    				String jobTitle = tokens[12];
    
    				Staff staff = new Staff(firstName, lastName, emailAddress,address,phoneNumber,officeLocation,salary,jobTitle);
    
    				person.add(staff);
    
    
    			}
    
    			else if ( objectType.equalsIgnoreCase("Employee")){
    				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[2]), Integer.parseInt(tokens[3]),Integer.parseInt(tokens[4]));
    				String emailAddress = tokens[9];
    				String officeLocation = tokens[10];
    				int salary = Integer.parseInt(tokens[11]);
    
    				Employee em = new Employee ( firstName, lastName,emailAddress,address, phoneNumber,officeLocation,salary);
    
    				person.add(em);
    
    			}
    
    		}
    		return person;
    	}
    
    
    }


    Error I get
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    	at DataReader.readData(DataReader.java:59)
    	at DataBaseDriver.main(DataBaseDriver.java:20)
    Last edited by HeavyTech; 06-01-2014 at 07:19 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Adding a Phone object into a Student, Employee objects.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at DataReader.readData(DataReader.java:59)
    at DataBaseDriver.main(DataBaseDriver.java:20)
    At line 59 the code used an index(=6) that was past the end of the array. Remember that array indexes range in value from 0 to the array length-1.
    Look at line 59 to see why it used an index past the end of the array.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Adding a Phone object into a Student, Employee objects.

    Hi,

    for debugging purpose I would suggest that you store the result of getLine() inside a variable. Then you add try/catch expressions so you can print out an error message together with the line that you tried to parse.

    Another point that I saw:
    It seems that you made a few copy&paste errors. Just look at the staff part:
    token[1] is the forename. but later you use it for the phone, too.

    My last comment is just a suggestion: I would refactor the code.
    - Your method is quite long - split it up into more parts.
    - You have an object oriented language - just make your code object oriented. So if you have an array of strings and you want to create an object from it: That is a typical constructor or create method. And if the start position can be different to 0, then you should simply provide code inside your classes that do this instead having it at some location where it simply does not belong (in my eyes). If you do not want to put stuff into your business objects: We tend to use Adapter classes that contain such logic so we keep clean POJOs without additional code that we do not want. (That might not be the best practice but I like the result. So in your case I would have a Stuff pojo and a Stuff Adapter that has the required code to scan a Stuff instance from a String[].

    That are just a few thoughts. But they could make it much easier, because you split your code up in multiple classes which might be easy to test.

    With kind regards,

    Konrad

  6. #6
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default Re: Adding a Phone object into a Student, Employee objects.

    Another point that I saw:
    It seems that you made a few copy&paste errors. Just look at the staff part:
    Staff is correct. tokens[0] is always the object type.
    and the part with the phone number is where I am stuck at. The phone numbers always come in the next line of my text file. I tried storing phone numbers in another ArrayList, but I am having trouble matching it with the corresponding object. Which is the object in the person arraylist.



    You have an object oriented language - just make your code object oriented. So if you have an array of strings and you want to create an object from it: That is a typical constructor or create method. And if the start position can be different to 0, then you should simply provide code inside your classes that do this instead having it at some location where it simply does not belong (in my eyes). If you do not want to put stuff into your business objects: We tend to use Adapter classes that contain such logic so we keep clean POJOs without additional code that we do not want. (That might not be the best practice but I like the result. So in your case I would have a Stuff pojo and a Stuff Adapter that has the required code to scan a Stuff instance from a String[].
    Yes, my program is object oriented. I have 7 different classes all with toString() methods to make it look the way I want it..., but reading in the file, Text I/O is where I am having problem. I can't figure how to match the phonenumber along with the objects

    so my file looks something like this

    Java Code:
    Student, John, Doe, JohnDoe@univeristy.edu, "Address", Freshman,
    PhoneNumber, Cell,555,555,5555

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Adding a Phone object into a Student, Employee objects.

    Ok, I think I understood your Problem.

    So you really have to split up your code. If the phone comes on the next line, you need some kind of recursion when parsing the file.

    Konrad

  8. #8
    HeavyTech is offline Member
    Join Date
    Jun 2014
    Posts
    11
    Rep Power
    0

    Default Re: Adding a Phone object into a Student, Employee objects.

    ah, Recursion comes in our next java class.
    Well, let me keep trying.
    thanks

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Adding a Phone object into a Student, Employee objects.

    You are welcome.

    To solve your issue without recursion, you could copy the code again:
    - you need to get the next line and split it up
    - check the first token
    - create the phone instance

    That works, but imagine, that you need to support a higher depths. But better solutions will be shown later.

    Konrad
    HeavyTech likes this.

Similar Threads

  1. Replies: 0
    Last Post: 06-06-2012, 08:26 AM
  2. adding two objects
    By apiwowar in forum New To Java
    Replies: 4
    Last Post: 09-15-2011, 03:38 AM
  3. adding objects into arrayLists
    By socboy6579 in forum New To Java
    Replies: 19
    Last Post: 12-10-2010, 06:59 AM
  4. Method for adding and drawing several objects
    By Pillow in forum New To Java
    Replies: 2
    Last Post: 09-08-2010, 11:24 PM
  5. Replies: 1
    Last Post: 05-04-2010, 11:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •