I want to update a record of a gym member when his/her weight change. But unfortunately my program can't find the given member.

Here is my code:

public static void updateWeight()
    {
        Scanner in = new Scanner(System.in); // Creation of a new Scanner object
        String tempName = ""; // Store temporary name collected by Scanner objecgt
        String tempLastName = ""; // Store temporary last name collected by Scanner object
        boolean found = false; // Boolean value in the case tha a member is found
        int i = 0;

        //Message to be displayed on the screen to input the values of tempName and tempLastName
        System.out.println(" Give first name and last name of the member you want to change the weight separated with a space .");        
        String line = in.nextLine();
        String[] split = line.split(" ");
        tempName = split[0];
        tempLastName = split[1];
        //System.out.println(tempName + " " + tempLastName);   

           
        ListIterator itr = GymList.listIterator();
        
        while(itr.hasNext())
        {
            if( (tempName.equals(GymList.get(i).getFirstName())) && (tempLastName.equals(GymList.get(i).getLastName())))
            {
                System.out.print(" Give new value for weight in kg: "); //Ask for new weight value
                GymList.get(i).setWeight(in.nextDouble()); //Set new weight
                found = true; // change boolean value to true
                break;
            }
            itr.next(); // move iterator to next position
            i++; // increment counter
        }
        
        //if member found
        if(found)
        {
            System.out.println(); //Empty line
            System.out.print(" Updated data: "); //Message
            //Display updated data
            System.out.println(GymList.get(i).getFirstName() + " " + GymList.get(i).getLastName() + " " +
                               GymList.get(i).getWeight() + " " + GymList.get(i).groupBMI());
        }//in case that member is not found
        else
        {
            System.out.println(" No such member! ");
        }      
            
    }

Recommended Answers

All 19 Replies

what do you mean 'can't find the given member'?
where does GymList come from? how is the information stored in there?
is found actually set to true? (does he ask for a new weight?)

can you elaborate a bit on what the error message is if there is an error, where it happens, whats the output?

did you try setting a breakpoint on that if at line 22 and seeing what values are being compared and if you ever enter that if statement?

I have created a SortedLinkedList which collects objects of type member. So when I am searching in the SortedLinkedList I cannot find any member. The information is coming from a text file every other method of my program works fine.

yes, but how do you store it in that GymList?
just write a small loop to iterate over it and print the name of every element. see if it does what you think it should do

output to a file method:

public void output()
    {
        String file = "listMember.txt";  // Variable to store the name of the file
        char firstLetter ; // Variable to store the first letter of the name
        int i = 0;
        
        try
        {
            PrintWriter outputFile = new PrintWriter(file); // Creation a PrintWriter object
            
            ListIterator itr = GymList.listIterator(); // Iterator to iterate in the GymList
            
            outputFile.println("CLUB MEMBERS"); //Title to the file
            
            //for all the members in the SortedLinkeList
            while(itr.hasNext())
            {
                // Store the first letter of the first name
                firstLetter = GymList.get(i).getFirstName().charAt(0);
                outputFile.println(firstLetter + //Display letter
                                    ". " + GymList.get(i).getLastName() + //Display last name
                                    " " + GymList.get(i).groupBMI()); //Display group
                itr.next();
                i++;
            }
            outputFile.close(); //Close File
        }
        catch(Exception e)
        {
            System.out.println("Could not load file!");
        }
        System.out.println(" Data stored in the file: listMember.txt");
    }

So the contents of the output file are:

CLUB MEMBERS
N. Brown GROUP 1
H. Davies GROUP 4
M. Jones GROUP 2
J. Smith GROUP 2
M. Williams GROUP 3

So my read and add method works fine the problem is just with this method (update)

alright, you have a list of objects, you save 1 scanned string that you split with a space , then in a if you compare both parts to strings from the member objects in the list.

we can figure all that from your code and after a quick look, it seems fine, so please, help us help you, set a break point, put a spy on the line variable, the split array, and GymList. Tell us if they contain the data that they should contain.

If they do, move step-by-step towards the if statement at line 22, when you get there, ask yourself: "with the current data i now know my variables contain, should this if statement resolve to true and enter the code block, or false and skip it?". From there on before moving over the if put a spy on the first .eqals() call, a spy on the second .equals() call and another spy on the whole condition inside that if. Now compare what you had predicted and what the spies are saying, is it accurate? Proceed to move after the if and note if you enter the if's code block or skip it, was it supposed to enter or supposed to skip?

after inspection of that loop you use to move arround your members i noticed you use an iterator but keep a counter and fectch data with "i" ...

why are you using half of the iterator, try modifying your loop to use the actual correct members, your way probably works just as fine and thats not why you can't update the weights, but it's still a concept failure that should be fixed while were looking at that code.

// create an array list 
ArrayList al = new ArrayList(); 
// add elements to the array list 
al.add("C"); 
al.add("A"); 
al.add("E"); 
al.add("B"); 
al.add("D"); 
al.add("F"); 
// use iterator to display contents of al 
System.out.print("Original contents of al: "); 
Iterator itr = al.iterator(); 
while(itr.hasNext()) {

    Object element = itr.next(); 
    System.out.print(element + " ");

}

1)the split array, and GymList. Tell us if they contain the data that they should contain?

---> Yes the data is as I expect to be.
2) is it accurate?

----> if statement ignored(never enter in the if statement)

and you've used the same upper- lowercases?

and you've used the same upper- lowercases?

--> Yes

change:

while(itr.hasNext())
        {
            if( (tempName.equals(GymList.get(i).getFirstName())) && (tempLastName.equals(GymList.get(i).getLastName())))
            {

to

while(itr.hasNext())
        {

System.out.println("Stored name: " + GymList.get(i).getFirstName() + " " + GymList.get(i).getLastName());

            if( (tempName.equals(GymList.get(i).getFirstName())) && (tempLastName.equals(GymList.get(i).getLastName())))
            {

maybe like this you can see something different.

From the terminal :

Joan Smith
Stored name: Nigel  Brown 
Stored name: Henry  Davies 
Stored name: Martin  Jones 
Stored name: Joan  Smith 
Stored name: Mary  Williams 
 No such member!

The member exists what is going wrong?

also if you program the "search" algorithm ONCE and implement it in a method then you could reuse it and save yourself some lines in every other methods...

public int indexOf(string firstName, string lastName){
    int index = -1;
    ListIterator itr = GymList.listIterator();
    int i = 0;

    Member temp;
    while(itr.hasNext()){
        temp = (Member)(itr.next());
        if( temp.getFirstName().equals(firstName) && temp.getLastName().equals(lastName) ){
            index = i;
            break;
        }
        i++;
    }
    return index; //if this returns -1 then theres no member with this name
}

public void changeWeight(){
    //promt input and stuff
    int index = indexOf( firstNameEntered , lastNameEntered);
    if(index != -1){
        //change weight of GymList.get(index)
    }
}

maybe there's a space somewhere in your names. try:

...
((tempName.trim()).equals((GymList.get(i).getFirstName())).trim())

maybe there's a space somewhere in your names. try:

...
((tempName.trim()).equals((GymList.get(i).getFirstName())).trim())

It seems to work but when I remove break; statement the while loop is infinite and with break i receive :
unreachable statement for:

itr.next(); // move iterator to next position
            i++; // increment counter

Any suggestions?

not really sure what you mean .. can you show a bit more about the code you have now?

public static void updateWeight()
    {
        Scanner in = new Scanner(System.in); // Creation of a new Scanner object
        String tempName = ""; // Store temporary name collected by Scanner objecgt
        String tempLastName = ""; // Store temporary last name collected by Scanner object
        boolean found = false; // Boolean value in the case tha a member is found
        int i = 0;

        //Message to be displayed on the screen to input the values of tempName and tempLastName
        System.out.println(" Give first name and last name of the member you want to change the weight separated with a space .");        
        String line = in.nextLine();
        String[] split = line.split(" ");
        tempName = split[0];
        tempLastName = split[1];
         

           
        ListIterator itr = GymList.listIterator();
        
        while(itr.hasNext())
        {
            
            //System.out.println("Stored name: " + GymList.get(i).getFirstName() + " " + GymList.get(i).getLastName());
            
            if((tempName.trim()).equals((GymList.get(i).getFirstName().trim())) && ((tempLastName.trim()).equals(GymList.get(i).getLastName().trim())));
            {
                System.out.print(" Give new value for weight in kg: "); //Ask for new weight value
                GymList.get(i).setWeight(in.nextDouble()); //Set new weight
                found = true; // change boolean value to true

                break;
            }
              
            itr.next(); // move iterator to next position
            i++; // increment counter

        }

            
        if(found)
         {
            System.out.println(); //Empty line
            System.out.print(" Updated data: "); //Message
            //Display updated data
            System.out.println(GymList.get(i).getFirstName() + " " + GymList.get(i).getLastName() + " " +
                               GymList.get(i).getWeight() + " " + GymList.get(i).groupBMI());
        }else
        {
             System.out.println(" No such member! ");
        }                    
        //in case that member is not found
          
        //if member found
        
            
    }

unreachable statement for:

itr.next(); // move iterator to next position
    i++; // increment counter

you have a semi-colon after your if statement, making the block afterwards execute everysingle time, and thus doing the "break;" everytime , which exits the while. That is why the itr.next(); is unreachable.

Thanks both of you for your help

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.