User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,535 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,340 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1495 | Replies: 46
Reply
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Help Primary Key pls help!

  #1  
Feb 7th, 2008
I am trying to do a primary key so that each member can't hava the same ID and if th same ID is entered an error is give. Can someone please help me cause I can't do it at all.Thank you.

  static void addMember(ArrayList<GymUser> UserListIn)
    {


        String tempUserrID;
        String tempID;
        String tempName;
        String tempSurname;
        int tempAge;
        String tempAddress;
        int tempPhone;
        String tempDuration;
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");
        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();
        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();
        UserListIn.add(new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration));
        
       
    }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: Primary Key pls help!

  #2  
Feb 7th, 2008
Inside the: UserListIn.add(GymUser newGymUser) method you will take the new Gym User and compare him with the other gymUsers. If one with the same id already exists then don't add him and print a message or throw an Exception.
If you don't want to make this check inside the .add method, then before calling it, take all the other users and compare them with the new user. If the new user doesn't have the same id, then call your method.
By the way, if you are using a database, and the table already is created with a primary key then you don't need to check anything because an SQLException will be thrown and the new user won't be inserted
I AM the 12th CYLON
Reply With Quote  
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Help Re: Primary Key pls help!

  #3  
Feb 7th, 2008
No for this database I am using java. I know database are generally coded with SQL but in my assigment I need to use java. Can you help me a bit in the coding and where to put it because I'm still a beginner with this type of coding. Thank you very much javaAddict.
Reply With Quote  
Join Date: Mar 2004
Posts: 732
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 31
Phaelax Phaelax is offline Offline
Master Poster

Re: Primary Key pls help!

  #4  
Feb 7th, 2008
I suppose you could just use a HashMap
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: Primary Key pls help!

  #5  
Feb 7th, 2008
I already told you where to put it. Just create a method that returns boolean and has a GymUser argument, that checks whether the input gymUser's id already exists among the other users that are already stored. By the way, where do you store the other users?(ArrayList, Vector?)

GymUser newGymUser = new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
boolean b = alreadyExists(newGymUser );
if (b) {
  System.out.println("Cannot insert existing user");
} else {
 UserListIn.add(newGymUser );
}
I AM the 12th CYLON
Reply With Quote  
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Re: Primary Key pls help!

  #6  
Feb 7th, 2008
I don't know how to use hash maps. I'm that good in java. All I want to do I think is to compare the allready saved ID no to the new ID being inputted. But I'don't know how to code the thing and where to place it.
Reply With Quote  
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Help Re: Primary Key pls help!

  #7  
Feb 7th, 2008
In an array List.
Reply With Quote  
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Help Re: Primary Key pls help!

  #8  
Feb 7th, 2008
I've done this till now from the help given form you both. But now if I will have an error (cannot find symbol - variable newGymUser). I am storing these users in an array list. Thank you very much javaAddict....but I'm still having errors.Can oyu help me pls. Thanks

      static void addMember(ArrayList<GymUser> UserListIn)
    {
        String tempUserrID;
        String tempID;
        String tempName;
        String tempSurname;
        int tempAge;
        String tempAddress;
        int tempPhone;
        String tempDuration;
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");
        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();
        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();
        UserListIn.add(new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration));
        

        boolean b = alreadyExists(newGymUser );     /error here
        if (b) {
                    System.out.println("Cannot insert existing user");
                } else 
                {
                        UserListIn.add(newGymUser );
                }
                GymUser newGymUser = new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
                boolean b = alreadyExists(newGymUser );
                if (b) {
                            System.out.println("Cannot insert existing user");
                        } else 
                        {
                            UserListIn.add(newGymUser );
                        }

       
    }
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 537
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 57
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: Primary Key pls help!

  #9  
Feb 7th, 2008
You will have an ArrayList. (Read the API) Before you do an add, you will read all the existing users and compare their IDs with the new one. If it does not exist then inserted.

http://java.sun.com/j2se/1.5.0/docs/...w-summary.html

Try the util package
I AM the 12th CYLON
Reply With Quote  
Join Date: Feb 2008
Posts: 39
Reputation: javaStud is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
javaStud javaStud is offline Offline
Light Poster

Help Re: Primary Key pls help!

  #10  
Feb 7th, 2008
Sorry asking many quest but,it means that I need the comparing..(those few coding that you gave me) before adding in the array list? Thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 5:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC