Home > Software engineering >  Why are the correct array values not getting printed?
Why are the correct array values not getting printed?

Time:11-05

I'm trying to print the marks of the rollNumber array using only nested if else. However, the output that I'm getting is:

111 Honors
111 First Division
333 Fail

Whereas the output should be

111 Honors
222 First Division
333 Fail
444 Second Division

Where am I going wrong? The code that I've written is stated below:

public class JavaApplication53 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int rollNumber[]={111, 222, 333, 444};
        int marks[]={81, 75, 43, 58};
        
        for(int i=0; i<rollNumber.length; i  ) {
            if(marks[i]>49) {
                if(marks[i]>79) {
                    System.out.println(rollNumber[i]   " Honors");
                
                    if(marks[i]>59){
                        System.out.println(rollNumber[i]   " First Division");
                    } else {
                        System.out.println(rollNumber[i]   " Second Division");
                    }
                }
            } else {
                System.out.println(rollNumber[i]  " Fail");
            }
        }
    }
}

CodePudding user response:

Your logic is off. If you've already tested for >79, it does not make sense to test for >59 in the if clause. Use the else clause instead. The innermost if should be in the else of the if it is in.

if(marks[i]>79) {
......
} else {
    //move this if from the if clause to here: else clause
    if(marks[i]>59){
      .......
    }
}

Alternatively, it pays to be systematic so you do not confuse yourself. You started off with testing the lowest points - 49 - and then followed that by testing the highest points - 79. Maybe 49, 59, 79 would avoid confusion as follows:

if(marks[i]>49) {
    if(marks[i]>59) {
        if(marks[i]>79) {
            System.out.println(rollNumber[i]   " Honors");
        } else {
            System.out.println(rollNumber[i]   " First Division");
        }
    } else {
        System.out.println(rollNumber[i]   " Second Division");
    }
} else {
    System.out.println(rollNumber[i]  " Fail");
}

OR:

if(marks[i]>79) {
    System.out.println(rollNumber[i]   " Honors");
} else if(marks[i]>59) {
    System.out.println(rollNumber[i]   " First Division");
} else if(marks[i]>49) {
    System.out.println(rollNumber[i]   " Second Division");
} else {
    System.out.println(rollNumber[i]  " Fail");
}

CodePudding user response:

I've improved your code. Use this.

public static void main(String[] args) {
    int rollNumber[]={111, 222, 333, 444};
    int marks[]={81, 75, 43, 58};
    
    for(int i=0; i<rollNumber.length; i  ) {
        if(marks[i]>79) {
            System.out.println(rollNumber[i]   " Honors");
        } else if(marks[i]>59) {
            System.out.println(rollNumber[i]   " First Division");
        } else if(marks[i]>49) {
            System.out.println(rollNumber[i]   " Second Division");
        } else {
            System.out.println(rollNumber[i]   " Fail");
        }
    }
}

Hope the problem gets better!

CodePudding user response:

I don't know what the expected behavior is really, but this is clearly an issue:

if(marks[i]>79) {
    System.out.println(rollNumber[i]   " Honors");                
        if(marks[i]>59){
            System.out.println(rollNumber[i]   " First Division");
        } else {
            System.out.println(rollNumber[i]   " Second Division");
        }
        

if marks[i] > 79 it will always be > 59, so the else will never be entered

EDIT: Instead of working with arrays and using the index i to take the corresponding values from them, you could create a domain model using classes:

class Category {
    final String name;
    final int boundary;

    Category(String name, int boundary) {
        this.name = name;
        this.boundary = boundary;
    }
}

class Grade {
    final int mark;
    final int rollNumber;

    Grade(int mark, int rollNumber) {
        this.mark = mark;
        this.rollNumber = rollNumber;
    }
}

(getters and setters omitted)

This can clarify the meaning of your code.

  • Related