In this method I am trying to locate a user by its ID and then remove it. I am having errors in the second if statment (cannot find symbol-method getID() ).

static void removeMember(ArrayList<GymUser> UserListIn)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.print("Enter the ID no of the member to be removed: ");
        String ID = keyboard.next();
        int location = -1;
        if (UserListIn.size() == 0){
            System.out.println("You have no members to remove!");
        }
        else {
            for (int i=0; i<=UserListIn.size(); i++)
            {
            	if (UserListIn.getID() == ID)              //having error
            	{
            	   location = i;
            	   break;
            	}
                //location = UserListIn.indexOf(new GymUser(ID,null, null,null,null,null,null,null));
            }
            if (location == -1)
            {
                System.out.println("The ID No. you have entered does not exist in this database!");
            }
            else UserListIn.remove(location);
        }
    }

//class that contains methods..

import java.io.*;
public class GymUser implements Serializable
{
        //these are instance variables
        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 String getM_ID()
       {
           return m_UserrID;
       }
       // method to get the membership duration
       public String getDuration()
       {
           return m_UserDuration;
       }
       
       // method to get the name
       public String getName()
       {
           return m_UserName;
       }
           
       //method to set name
       public void setName(String UserName)
       { 
            m_UserName = UserName;
       }
           
        
       
        //method to return age
        public int getAge()
        {
            return m_UserAge;
            }
            
          //method to set age
        public void setAge(int UserAge)
        {
            m_UserAge =UserAge;
            }   
            
        
        //method to return address
        public String getAddress()
        {
            return m_UserAddress;
            }
            
         
         //method to set address
        public void setAddress(String UserAddress)
        {
            m_UserAddress=  UserAddress;
            }
            
        //method to return phone
        public int getPhone()
        {
        	return m_UserPhone;
        }
        
        //method to set phone
        public void setPhone(int UserPhone)
        {
        	m_UserPhone = UserPhone;
        }
        
        //method to return suname
        public String getSurname()
        {
        	return m_UserSurname;
        }
        
        //method to set surname
        public void setSurname(String UserSurname)
        {
        	m_UserSurname = UserSurname;
        }
        
        //method to get name
        public String getID()
        {
        	return m_UserID;

        }

Recommended Answers

All 12 Replies

Because "UserListIn" is an ArrayList of GymUser objects, it is not a GymUser object itself. You need to retreive the GymUser object from the ArrayList using get(i), then call getID() on that.

I am confusing the part to get(i). I am not understanding it to much(how I am going to code it). sorry I am a beginner in java. Thanks already for your help masijade . Can you pls help me a bit with the coding because I am confusing it.

UserListIn.get(i) will return object of i'th position in this list
for more info see this

Now I've retrieved the GymUser object from the ArrayList using get(i) as you told me. Now I think I have to change the statment in if.Am I right...although I don't know what how.

static void removeMember(ArrayList<GymUser> UserListIn)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.print("Enter the ID no of the member to be removed: ");
        String ID = keyboard.next();
        int location = -1;
        if (UserListIn.size() == 0){
            System.out.println("You have no members to remove!");
        }
        else {
            for (int i=0; i<=UserListIn.size(); i++)
            {
                UserListIn.get(i);
            	if (UserListIn.getID() == ID)
            	{
            	   location = i;
            	   break;
            	}
               location = UserListIn.indexOf(new GymUser(ID,null, null,null,null,null,null,null));
            }
            if (location == -1)
            {
                System.out.println("The ID No. you have entered does not exist in this database!");
            }
            else UserListIn.remove(location);
        }
    }

do this:
inside removeMember get one member like this-
GymUser gu= UserListIn.get(i);
use this gu in place of UserListIn;

I've tried what you've told me DangerDev and I have no syntax errors. Now when I am running the method and I am giving the ID no. to be removed I am having an exception (IndexOutOfBoundsException Index: 2 Size:2 i java.util.ArrayList ) can you helo me pls.

Thank you.

This is what I tried because I forgot to give you the code.

static void removeMember(ArrayList<GymUser> UserListIn)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.print("Enter the ID no of the member to be removed: ");
        String ID = keyboard.next();
        int location = -1;
        if (UserListIn.size() == 0){
            System.out.println("You have no members to remove!");
        }
        else {
            for (int i=0; i<=UserListIn.size(); i++)
            {
                GymUser gu= UserListIn.get(i);

                //UserListIn.get(i);
            	if (gu.getID() == ID)
            	{
            	   location = i;
            	   break;
            	}
              // location = UserListIn.indexOf(new GymUser(ID,null, null,null,null,null,null,null));
            }
            if (location == -1)
            {
                System.out.println("The ID No. you have entered does not exist in this database!");
            }
            else UserListIn.remove(location);
        }
    }

UserListIn.size() will return total no. of element in array(not index of last item), so put i< not i<= in for statement.

OW sorry DangerDev I am asking so much but now I had no exceptions that I've tried as you told me. Now when I am giving the ID no. to be removed I am having the message "The ID No. you have entered does not exist in this database! " Why? I'm sure it exists!! Thank you very much for your help. Do you know why pls?

static void removeMember(ArrayList<GymUser> UserListIn)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.print("Enter the ID no of the member to be removed: ");
        String ID = keyboard.next();
        int location = -1;
        if (UserListIn.size() == 0){
            System.out.println("You have no members to remove!");
        }
        else {
            for (int i=0; i<UserListIn.size(); i++)
            {
                GymUser gu= UserListIn.get(i);

                //UserListIn.get(i);
            	if (gu.getID() == ID)
            	{
            	   location = i;
            	   break;
            	}
              // location = UserListIn.indexOf(new GymUser(ID,null, null,null,null,null,null,null));
            }
            if (location == -1)
            {
                System.out.println("The ID No. you have entered does not exist in this database!");
            }
            else UserListIn.remove(location);
        }
    }

hi
use gu.getID().equals(ID)
in place of gu.getID() == ID, here u r comparing string not nums.

Thanks DangerDev!!! you're great.!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.