Search code examples
javaarraysnetbeansuser-input

Challenges with arrays


This is what I have to do:
Display the numbers of students in each of the Levels (R, 1, 2, 3, 4).

Level R (mark < 50)     
Level 1 ( (mark >= 50) && (mark < 60) )    
Level 2 ( (mark >= 60) && (mark < 70) )     
Level 3 ( (mark >= 70) && (mark < 80) )    
Level 4 (mark >= 80). 

This is what I have:

int L0=0,L1=0,L2=0,L3=0,L4=0;
    int acceptmarks=0;
        acceptmarks=Integer.parseInt(marksinput.getText());
    for(int i=0; i < marks.size(); i++){
    if (acceptmarks<50){
        L0=L0+1;}
    else if ((acceptmarks>=50) && (acceptmarks<60)){
        L1=L1+1;}
    else if ((acceptmarks>=60) && (acceptmarks<70)){
        L2=L2+1;}
    else if ((acceptmarks>=70) && (acceptmarks < 80)){
        L3=L3+1;}
    else if ((acceptmarks>=80) && (acceptmarks < 100)){
        L4=L4+1;}}
    int[] level= new int [5];
    level[0]=L0;
    level[1]=L1;
    level[2]=L2;
    level[3]=L3;
    level[4]=L4;
        analyzeoutput.setText("Numbers at Level 4:" + level[4]+ "\nNumbers at Level 3:" + 
            level[3]+ "\nNumbers at Level 2:" + level[2]+ "\nNumbers at Level 1:" + level[1]+ "\nNumbers at Level R:" + level[0]);

The problem I'm having is it does not displays the correct number of students for each level.

This is what happens:
Ex. I input 4 marks: 45, 66, 84, 89.
Correct answer should be 1 mark in Level R, 1 in Level 2 and 2 in Level 4. But my program just counts the last mark. Since, last mark is 89(level 4), it displays all marks has level 4. I have spent hours on this.
Any help is GREATLY APPRECIATED and thank you in advance.

EDIT: I figured it out. I just needed to replace acceptmarks with marks.get(i) in the if statements.


Solution

  •     int L0=0,L1=0,L2=0,L3=0,L4=0;
        List<Integer> marks = Arrays.asList(45, 66, 84, 89);
        for(int i = 0; i < marks.size(); i++){
            if (marks.get(i)<50){
                L0=L0+1;}
            else if ((marks.get(i)>=50) && (marks.get(i)<60)){
                L1=L1+1;}
            else if ((marks.get(i)>=60) && (marks.get(i)<70)){
                L2=L2+1;}
            else if ((marks.get(i)>=70) && (marks.get(i) < 80)){
                L3=L3+1;}
            else if ((marks.get(i)>=80) && (marks.get(i)< 100)){
                L4=L4+1;}}
        int[] level= new int [5];
        level[0]=L0;
        level[1]=L1;
        level[2]=L2;
        level[3]=L3;
        level[4]=L4;
    
    
        final JLabel analyzeoutput = new JLabel();
        analyzeoutput.setText("Numbers at Level 4:" + level[4]+ "\nNumbers at Level 3:" +
                level[3]+ "\nNumbers at Level 2:" + level[2]+ "\nNumbers at Level 1:" + level[1]+ "\nNumbers at Level R:" + level[0]);
        }
    

    Note: If you can put all input that you are getting in "acceptmarks" to marks array. this will work.

    AcceptMarks is an integer so it will contain only last input that you have given. either make AcceptMarks as a list or use marks as the array as shown in code snippet. I am using marks because you are looping on marks.