•
•
•
•
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
![]() |
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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));
} 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
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
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
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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 );
}
} 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
http://java.sun.com/j2se/1.5.0/docs/...w-summary.html
Try the util package
I AM the 12th CYLON
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- help pls.. database related! (VB.NET)
- eliminate duplicate items in combobox! pls help! (Visual Basic 4 / 5 / 6)
- Default value of a column (MS SQL)
Other Threads in the Java Forum
- Previous Thread: approximating pi
- Next Thread: Gregorian Calendar...need help pls.


Linear Mode