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.
index++;/** * 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]
}
}
return foundClimber;
}
}
[/CODE]