Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Difficulty searching element in an Array

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difficulty searching element in an Array

    Been messing around for hours and eventually decided to seek the assistance of the internet.

    I am trying to make a program where I/the user can add elements into an array (Name, age gender). Once elements have been added to the array, I want to then be able to search a String (name) to find the desired name I am after in the array. The context of the program is adding Climbers to a Climber Database (just an array) of who has climbed what mountain (haven't attempted adding the mountain stuff yet).

    In what I've written so far, I keep getting the return null, saying that the searched String cannot be found. I have a feeling that I am failing to pass the parameters of the added element in the array.
    For example, I add a 'climber' with the name John, 19, Male in the Climber Class. I then go to the ClubStats class to getClimber and search "John", the only one in the array and I receive a null..

    FYI I am using BlueJ here so at a serious basic level.

    /**
     * Write a description of class Climber here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class Climber
    {
        // Instance variables.
        // The climber name.
        private String name;
        // The climber age.
        private int age;
        // The climber gender.
        private String gender;
     
        /**
         * Constructor for objects of class Climber
         */
        public Climber(String newName, int newAge, String newGender)
        {
            // Initialise instance variables.
            name = newName;
            age = newAge;
            gender = newGender;
        }
     
        /**
         * Accessor method for climber's name.
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * Set the climber's name.
         */
        public void setName(String newName)
        {
            name = newName;
        }
     
        /**
         * Accessor method for climber's age.
         */
        public int getAge()
        {
            return age;
     
        }
     
        /**
         * Set the climber's age.
         */
        public void setAge(int newAge)
        {
            age = newAge;
     
        }
     
         /**
         * Set the climer's gender.
         */
       public String getGender()
       {
           return gender;
       } 
     
       /**
         * Accessor method for climber's gender.
         */
        public void getGender(String newGender)
        {
            gender = newGender;
     
        }
     
    }
     
    import java.util.ArrayList;
    /**
     * Write a description of class ClubStats here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class ClubStats
    {
        // An ArrayList for storing climber details.
        private ArrayList<Climber> climber;
        // An ArrayList for storing mountain details.
     
        /**
        * Constructor for objects of class ClubStats
        */
        public ClubStats()
       {
           // Initialise instance variables.
           climber = new ArrayList<Climber>();
     
        }
     
        public void addClimber(Climber newName)
        {
            climber.add(newName);
        }
     
       public Climber getClimber(String name)
       {
           Climber foundClimber = null;
           int index = 0;
           boolean searching = true;
     
           while(searching && index < climber.size()) {
               Climber newName = climber.get(index);
               if(newName.equals(name)) {
                       searching = false;
                       foundClimber = newName;
                    }
                    else {
                        System.out.println(name + " not found");
               [CODE]
    index++;
    }
    }
    return foundClimber;
    }
    }

    [/CODE]

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Difficulty searching element in an Array

    Hi,

    Your 'newName" variable is not a String object, it's a Climber object. What you need is to compare the climber's name to the passed in name.

    I also strongly suggest renaming your 'climber' array list to 'climbers' - it's a good convention to use plural when naming lists or arrays.

    Climber climber = climbers.get(index);
    if (climber.getName().equals(name)) {
        // Found!
    }
    Good luck!

Similar Threads

  1. Searching a 2d Array for lines of 3
    By Lurie1 in forum Loops & Control Statements
    Replies: 4
    Last Post: January 20th, 2014, 02:00 PM
  2. [SOLVED] Help with searching an array for a String (name) using a for loop
    By seph in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2013, 09:01 PM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM