0

My code is to run a java program that contains several scores in an array and print them out according to their rangescore.

this is my code:

package Assignment.Q070;


import java.io.File;
import java.util.Scanner;

public class Q070 {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);

        int rangescore[] = {
            76,
            89,
            150,
            135,
            200,
            76,
            12,
            100,
            150,
            28,
            178,
            189,
            167,
            200,
            175,
            150,
            87,
            99,
            129,
            149,
            176,
            200,
            87,
            35,
            157,
            189
        };

        int score = 0;

        for (int i = 0; i < rangescore.length; i++) {
            score = rangescore[i];
        }

        while (input.hasNextLine()) {

            if (score >= 0 && score <= 24)
                rangescore[0]++;
            else if (score >= 25 && score <= 49)
                rangescore[1]++;
            else if (score >= 50 && score <= 74)
                rangescore[2]++;
            else if (score >= 75 && score <= 99)
                rangescore[3]++;
            else if (score >= 100 && score <= 124)
                rangescore[4]++;
            else if (score >= 125 && score <= 149)
                rangescore[5]++;
            else if (score >= 150 && score <= 174)
                rangescore[6]++;
            else if (score >= 175 && score <= 200)
                rangescore[7]++;
            else
                System.out.print("\nError occured ");
        }

        System.out.print("  Range Student  \n");
        System.out.print("     0 -  24:    \n" + rangescore[0]);
        System.out.print("    25 -  49:    \n" + rangescore[1]);
        System.out.print("    50 -  74:    \n" + rangescore[2]);
        System.out.print("    75 -  99:    \n" + rangescore[3]);
        System.out.print("   100 - 124:    \n" + rangescore[4]);
        System.out.print("   125 - 149:    \n" + rangescore[5]);
        System.out.print("   150 - 174:    \n" + rangescore[6]);
        System.out.print("   175 - 200:    \n" + rangescore[7]);
    }

}

I am using Netbeans Apache IDE 12.4 version.

when I compile my program, there is no error shows, and it shows that the compiler is running. please help me solve this problem.

6
  • 4
    It probably waits for you to input something in the terminal Commented Jul 7, 2021 at 12:49
  • I think you have to remove input from while loop. Commented Jul 7, 2021 at 12:53
  • You are waiting for scanner to finish reading but you are not reading, you are not consuming your stream. You keep checking if input has next line but it will run forever in the while because you are not advancing the scanner Commented Jul 7, 2021 at 12:53
  • What do you expect from console? I think your implementation not even close to what you want to achieve. Commented Jul 7, 2021 at 12:54
  • so how and which want shall i edit it to run the code? Commented Jul 7, 2021 at 12:58

1 Answer 1

1

There are 2 major issues with your code:

  1. You do not have a reason to wait for the input. If you do, then please explain clearly why you used scanner.

  2. You seem to want to classify scores with your specified ranges. However, you are manipulating the score array. Nothing makes sense here.

Following code block can solve your problem:

public void doSomethingAfterStartup() {

        int scores[] = {
                76,
                89,
                150,
                135,
                200,
                76,
                12,
                100,
                150,
                28,
                178,
                189,
                167,
                200,
                175,
                150,
                87,
                99,
                129,
                149,
                176,
                200,
                87,
                35,
                157,
                189
        };

        int ranges[] = new int[8];


        for (int i = 0; i < scores.length; i++) {
            int score = scores[i];
            if (score >= 0 && score <= 24)
                ranges[0]++;
            else if (score >= 25 && score <= 49)
                ranges[1]++;
            else if (score >= 50 && score <= 74)
                ranges[2]++;
            else if (score >= 75 && score <= 99)
                ranges[3]++;
            else if (score >= 100 && score <= 124)
                ranges[4]++;
            else if (score >= 125 && score <= 149)
                ranges[5]++;
            else if (score >= 150 && score <= 174)
                ranges[6]++;
            else if (score >= 175 && score <= 200)
                ranges[7]++;
            else
                System.out.print("\nError occured ");
        }

        System.out.print("  Range Student  \n");
        System.out.print("     0 -  24  :" + ranges[0] + "\n");
        System.out.print("    25 -  49  :" + ranges[1] + "\n");
        System.out.print("    50 -  74  :" + ranges[2] + "\n");
        System.out.print("    75 -  99  :" + ranges[3] + "\n");
        System.out.print("   100 - 124  :" + ranges[4] + "\n");
        System.out.print("   125 - 149  :" + ranges[5] + "\n");
        System.out.print("   150 - 174  :" + ranges[6] + "\n");
        System.out.print("   175 - 200  :" + ranges[7] + "\n");
    }

Output:

Range Student  
     0 -  24  :1
    25 -  49  :2
    50 -  74  :0
    75 -  99  :6
   100 - 124  :1
   125 - 149  :3
   150 - 174  :5
   175 - 200  :8
Sign up to request clarification or add additional context in comments.

1 Comment

i change another before it but it shows different answer. thank you very much for the soltuion.

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.