Konark Baggal
Konark Baggal

Reputation: 5

Why are the correct array values not getting printed?

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");
            }
        }
    }
}

Upvotes: 0

Views: 77

Answers (3)

Adriaan Koster
Adriaan Koster

Reputation: 16209

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.

EDIT 2: And then you can do this

  Category[] categories = new Category[]{
        new Category("Honors", 80),
        new Category("First Division", 60),
        new Category("Second Division", 50),
        new Category("Fail", Integer.MIN_VALUE)
    };

    Grade[] grades = new Grade[]{
        new Grade(81, 111),
        new Grade(75, 222),
        new Grade(43, 333),
        new Grade(58, 444)
    };

    for (Grade grade : grades) {
        for (Category category : categories) {
            if (grade.mark >= category.boundary) {
                System.out.printf("%s %s%n", grade.rollNumber, category.name);
            }
        }
    }

Upvotes: 0

PeterKA
PeterKA

Reputation: 24638

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");
}

Upvotes: 1

Sunghyeon Lee
Sunghyeon Lee

Reputation: 43

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!

Upvotes: 1

Related Questions