1

I am working with classes and arrayLists, i want to update an element(object) in the ArrayList. fist i searched for it, by the ID number. then I reseted the object members using a method in the class that prompt user for input..

the problem is, it update all the objects in the list not one in particular.

my work

if (studentList.isEmpty())
    System.out.println("\t\tNO DATA TO UPDATE !!");
else {
    try {
        System.out
                .print("\n\tTo Update info, Please Enter Students ID: ");
        System.out.flush();
        searchID = obj.readLine();
        for (Iterator<Student> it = studentList.iterator(); it
                .hasNext();) {
            Student S1 = it.next();
            if (searchID.equals(S1.getID()))
                System.out.print("\n\tEnter New Data: ");
            S1.setting();
        }
    } catch (Exception e) {
        // empty
    }
}

it asks to update all the objects instead of one. the update works but not the way i want...

3 Answers 3

2

You should have a block after your condition :

                        if (searchID.equals(S1.getID())) {
                            System.out.print("\n\tEnter New Data: ");
                            S1.setting();
                        }

Otherwise, you'll always call S1.setting(), regardless of the result of the condtion, since without the curly braces, the if statement only controls whether or not System.out.print("\n\tEnter New Data: "); will be executed.

Sign up to request clarification or add additional context in comments.

Comments

1

I've put it through my code cleaner and below is what it shows like:

if (studentList.isEmpty()) {
    System.out.println("\t\tNO DATA TO UPDATE !!");
} else {
    try {
        System.out.print("\n\tTo Update info, Please Enter Students ID: ");
        System.out.flush();
        searchID = obj.readLine();
        for (final Iterator<Student> it = studentList.iterator(); it.hasNext();) {
            final Student S1 = it.next();
            if (searchID.equals(S1.getID())) {
                System.out.print("\n\tEnter New Data: ");

                S1.setting();
            }
            // >>> this is where S1.setting() went wrong, moved up <<<                
        }
    } catch (final Exception e) {
        // empty
    }
}

The most important change is that all code blocks are with braces {}. If you do this it becomes very clear when a statement is executed. Without that only the first statement behind an if or else clause is executed. This makes it very easy to make coding mistakes as the one in the question.

Comments

0

If you want to conditionaly execute more than one line of code, you should put all of it betveen { ... }. If you leave it like this, only first one will depend on condition...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.