•
•
•
•
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,861 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 3,895 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: 1496 | Replies: 46
![]() |
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
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
I AM the 12th CYLON
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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));
} 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.
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.
Here is a sample of your code:
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()
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()
I AM the 12th CYLON
•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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));
}•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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 );
}
}•
•
Join Date: Feb 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 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 is the other class.
/**
* 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 );
}
}![]() |
•
•
•
•
•
•
•
•
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