Results 1 to 10 of 10

Thread: vector problem

  1. #1
    shinichi90 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default vector problem

    hi!! i'm new to java and i would like to ask some questions ^^

    i'm currently doing an assignment and i stumble upon some few problems...

    Java Code:
    import java.util.*;
    
    public class PassengerDatabase
    {
        private Vector<Passenger> vPassengers;
        private Scanner scan=new Scanner(System.in);
        
    
        public PassengerDatabase()
        {
            vPassengers = new Vector<Passenger>();
        }
    
        public void registerPassenger(Passenger passenger)
        {
             String passportID;
              System.out.print("PassportID: ");
    	passportID=scan.nextLine();
    
           //more input of passenger's information similiar to the above..
    
           vPassengers.add(passenger);
    
         }
    }
    
    public class Passenger{
    public void displayPassengerInfo(Passenger passenger)
        {
            String passportID;
    
            System.out.print("Enter passport ID: ");
            passportID=scan.nextLine();
    
            for(Passenger p:vPassengers)
            {
                if(p.getPassportID().equals(passportID))
                {
                    p.displayInformation();break;
                }
            }   
       }
    }
    
    public class Main
    {
       public static void main(String[] args){
    
           Passenger passenger=new Passenger();
           PassengerDatabase pDatabase=new PassengerDatabase();
        	int choice;
    	Scanner input = new Scanner(System.in);
    		do{
    			System.out.println("Please select an option: ");
    			System.out.println(" 1. Add passenger ");
    			System.out.println(" 2. view info");
    			System.out.println(" 3. Exit ");
    			System.out.println();
    			System.out.print("Option: ");
    			choice = input.nextInt();
    			switch(choice)
    			{
    				case 1:   pDatabase.registerPassenger(passenger);break;
    
    				case 2: pDatabase.displayPassengerInfo(passenger);break;
                            case 3:break;
                            }
    
                        }while(choice!=3);
        }
    }
    Passenger is another class where i do the setters and getters of passenger's info..i didnt put out the entire code as it was long and slightly messy...><

    the problem was that after i add a second Passenger...the first Passenger is missing from the vector....i'm not sure why....was there something wrong with my implementation of vector?

    thank you

    regards,
    shinichi90
    Last edited by shinichi90; 08-22-2010 at 06:35 PM.

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

    Default

    Try debugging your code by adding println()s to show how the variables are changing.
    For example show the size of the vector everytime you do something to it. You expect the vector's size to go from 0 to 1 to 2 etc.

    Also print out a message showing when the vector is created, in case you create more than one copy of it. Do this in the constructor where the vector is created.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    I think (but I can't be sure given your code) that you're only using one single Passenger object and you're adding that single object multiple times to your vector.

    kind regards,

    Jos

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

    Default

    Here's some test code and its output:
    Java Code:
          Vector vec = new Vector();
          vec.add(vec);
          System.out.println("vec.size=" + vec.size());
          vec.add(vec);
          System.out.println("vec.size=" + vec.size());
          vec.add(vec);
          System.out.println("vec.size=" + vec.size());
          //vec.size=1
          //vec.size=2
          //vec.size=3
    It looks like you can have multiple copies of the same thing in a Vector.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    I'm sure now; for a quick change add the following code:

    Java Code:
    public void registerPassenger(Passenger passenger) {
        passenger= new Passenger(); // <--- add this line
    kind regards,

    Jos

  6. #6
    shinichi90 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    thank you. it works now^^

    regards,
    shinichi90

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by shinichi90 View Post
    thank you. it works now^^

    regards,
    shinichi90
    Note that my solution was just a quick hack. If that method creates (and uses) a new Passenger object it needn't be passed in as a parameter and so, there is no need to create a Passenger object in your main( ... ) method.

    kind regards,

    Jos

  8. #8
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    Also, you should try to keep all the code that reads input from the user in one class. Right now, you have Scanner calls all over the place. Instead of asking for the Passport ID in the registerPassenger and displayPassengerInfo methods, you should ask for this in the Main class. You should then pass the Passport ID into those methods as a parameter.

  9. #9
    shinichi90 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by mangstadt View Post
    Also, you should try to keep all the code that reads input from the user in one class. Right now, you have Scanner calls all over the place. Instead of asking for the Passport ID in the registerPassenger and displayPassengerInfo methods, you should ask for this in the Main class. You should then pass the Passport ID into those methods as a parameter.
    i see....i thought that if i put everything in the main it would be quite messy as well..>< is it always good practice to only have user input in the main() instead of the other classes?

  10. #10
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by shinichi90 View Post
    i see....i thought that if i put everything in the main it would be quite messy as well..>< is it always good practice to only have user input in the main() instead of the other classes?
    The advantage to this is that it makes your code "loosely coupled". This means that the different components of your program are separated from each other. So, if you later decide that you want to change the way in which one component works, you only have to edit one or two classes instead of going over ALL the code in your ENTIRE program.

    What if you decided one day that instead of accepting input from the console, you wanted to write a GUI that allows the user to enter the input into textboxes? If your code was loosely coupled, then this would be a lot easier.

Similar Threads

  1. Problem with Vector
    By banhbaochay in forum New To Java
    Replies: 9
    Last Post: 04-01-2010, 05:01 PM
  2. Problem with vector
    By morghul in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 02:24 AM
  3. Static Vector Problem
    By calicocal in forum New To Java
    Replies: 12
    Last Post: 11-09-2008, 11:31 PM
  4. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 08:53 PM
  5. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:44 PM

Posting Permissions

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