1,105,332 Community Members

Primary Key pls help!

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
        
       
    }
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

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

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Phaelax
Practically a Posting Shark
880 posts since Mar 2004
Reputation Points: 52 [?]
Q&As Helped to Solve: 54 [?]
Skill Endorsements: 4 [?]
 
0
 

I suppose you could just use a HashMap

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

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 );
}
Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In an array List.

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 );
                        }

       
    }
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

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/api/overview-summary.html

Try the util package

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Can't you READ??? First you create a new object, then call the alreadyExists, and then call add.
You are calling add and then you are calling the alreadyExist and then you are doing another add. Does this make sense to you? By the way you are not creating the newGymUser object.

You have to implement the alreadyExists.

You have to create a global ArrayList where you will store the Gym Users objects

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

I will add some more code after a few hours, with better explanations

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you javaAddict. Sorry for asking many questions but I am still learning. At the moment I will try to follow your steps till you can explain me better. Thank you.

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried to do this but it's still not the way it should be. Having problems with (cannot find symbol- method already exists java.lang.string)Can you help with this pls? Thank you very much.

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();
       
         Object  newGymUser = new Object();
        
          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 );
                        }

        
        
        
        
        UserListIn.add(new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration));
        
      
       
    }
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Of course there is an error - you haven't written the method alreadyExists() yet. That is exactly what javaAddict was trying to tell you in the first place. You can't call methods that you haven't written yet.

Additionally, if you would actually read the API for HashMap, which Phaelex suggested, you would see that it has a method to see if a given key already exists, which makes it even easier on you.

If there is a requirement that you use ArrayList for an assignment, fine, that's valid. But if you're avoiding HashMap just because you don't know how to use it, that's just stubborn, lazy and ignorant.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Here is a sample of your code:

Object  newGymUser = new Object();
        
          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 );
                       }
        
        UserListIn.add(new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration));

Why do you call UserListIn.add() 3 times??????????????

And what is this: Object newGymUser = new Object();
The object is GymUser

Don't you understand what are you writing?
You are creating a new Object: Object newGymUser = new Object(); Checking if it exists in order to add it or not, AND then you do the same thing with :GymUser newGymUser = new GymUser(.....);
And after you are done, again you are calling the add.

Just create ONE object: GymUser newGymUser = new GymUser(.....); with arguments the ones that you have from the keyboard, and check this if it exists or not in order to add it.
And do it once.

I would suggest that you implement the bollean alreadyExists(GymUser user) in the class UserListIn. And if you are going to use ArrayList, or HashTable declare it in UserList and use methods to access it: like you are doing with add()

Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry that was not my exact code.I copied it twice by mistake. Now I think I have to write a method alreadyExists...yes? I've done the following:

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();
       

        
          boolean b = alreadyExists(newGymUser );
        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);
                
        
        
        
        
        UserListIn.add(new GymUser(tempUserrID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration));
        
      
       
    }
Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry I Changed the code a bit. Now I modified it as follows:

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();
       

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

      
       
    }
Member Avatar
javaStud
Junior Poster in Training
51 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

people I have this code in these class. I've now implemented the alreadyExists method but I don't know if it is correct. Now I am having an error in the same palce as before (cannot find symbol - method alreadyExist(GymUser) ) Can some please help me because I am really confused about this logic. Pls help me a bit. I know some of you are saying that they've already fed up helping me but I am really confused .Thanks.

/**
 * This class contains instance variables and method(members)
 * 
 */
import java.io.*;
public class GymUser implements Serializable
{
        //these are instance variables
            public String myPrimaryKeyField;

        private String m_UserrID;
        private String m_UserName;
        private String m_UserID;
        private String m_UserSurname;
        private String m_UserAddress;
        private int m_UserPhone;
        private int m_UserAge;
        private String m_UserDuration;
        
        //this is the constructor
        public GymUser(String UserrID,String UserName,String UserID,String UserSurname, String UserAddress, int UserPhone, int UserAge,String UserDuration)
        {
            m_UserrID =  UserrID;
            m_UserName = UserName;
            m_UserSurname = UserSurname;
            m_UserAddress = UserAddress;
            m_UserPhone = UserPhone;
            m_UserAge= UserAge;
            m_UserID = UserID;
            m_UserDuration = UserDuration;
        }
   public boolean alreadyExists (Object object)
    {
        if (this == object)
        {
            return true;
        }
         if (this != object)
        {
            return false;
        }
        
        GymUser other=(GymUser)object;
        return this.myPrimaryKeyField.equals(other.myPrimaryKeyField);



    }

This is the other class.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
     ArrayList<GymUser> UserListIn = new ArrayList<GymUser>()
    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();
       

        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration); 
        
          boolean b = alreadyExists(newGymUser );
        if (b) {
                    System.out.println("Cannot insert existing user");
                } else 
                {
                        UserListIn.add(newGymUser );
                }
}
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

That method does not remotely resemble what javaAddict explained. The method needs to loop through the entries in your collection checking each ID. If one of them has that ID then it should return true.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article