0

Question:

Find the number of D, C, B and A grades for the last test on informatics, where n students from a class have successfully passed the test.

In this task, we use a 5-point grading system and are interested only in passing grades: from 2 to 5. They correspond to the letter grades in the following way: 5 is for A, 4 is for B, 3 is for C and 2 is for D. The program gets number n as input and then gets the grades themselves: one by one.

The program should output four numbers in a single line: the number of D, C, B, and A grades respectively.

import java.util.Scanner;

class Main {
public static void main(String[] args) {
    // put your code here
    Scanner scan = new Scanner(System.in);

    int numStudents = scan.nextInt();
    int marks;

    int gradeA = 0;
    int gradeB = 0;
    int gradeC = 0;
    int gradeD = 0;

    for (int i = 0; i <= numStudents; i++){
        marks = scan.nextInt();
        if(marks == 5){
            gradeA++;
        } else if (marks == 4){
            gradeB++;
        } else if (marks == 3){
            gradeC++;
        } else if (marks == 1){
            gradeD++;
        }
    }

    System.out.println(gradeD);
    System.out.println(gradeC);
    System.out.println(gradeB);
    System.out.println(gradeA);
}

}

ERROR:

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Main.main(Main.java:17)
Sushant19
  • 21
  • 4
  • 3
    Does this answer your question? [Scanner error with nextInt()](https://stackoverflow.com/questions/12832006/scanner-error-with-nextint) – cy3er Jul 02 '20 at 07:11
  • No i think you should define marks = 0 – Enginner S. Saad Jul 02 '20 at 07:12
  • 1
    Does this help? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Abra Jul 02 '20 at 07:13
  • Does this answer your question? [Scanner next() throwing NoSuchElementException for some online compilers](https://stackoverflow.com/questions/39766488/scanner-next-throwing-nosuchelementexception-for-some-online-compilers) I can reproduce this issue in the online compiler where you have to print input data in the specific input area for standard input. – Alex Rudenko Jul 02 '20 at 07:30

4 Answers4

1

If you define numStudents as 5, then you should enter 6 (because of <= in your for loop)

for (int i = 0; i <= numStudents; i++){

This should be..

for (int i = 0; i < numStudents; i++){

When you enter 1 in stdin as first input, it will be numStudents. Now, you have to enter 2 inputs because of <=. So your stdin should be

1
2
3

Instead, better change <= to < in your code, so that you can enter 1 2

You get NoSuchElementException when you given only 1 2 in stdin with your code (with <=)

JavaTechnical
  • 5,807
  • 8
  • 40
  • 79
0

If there is no integer on the scanner, you will get this error. You have to put a condition. Try:

if(scan.hasNextInt()) { 
marks = scan.nextInt(); 
}
Pankaj
  • 67
  • 7
  • Although generally true that you should call `hasNextInt()` before calling `nextInt()`, it isn't actually the right thing to do for this type of code, unless you just want to provide a better error message. The input starts by e.g. saying "here are 5 values", followed by 5 values. If there aren't 5 values after the initial "count" value, then it is invalid input, and the default exception is as valid as any other way to handle that (for this type of program). --- Sorry, since this doesn't actually address the error in the code (bad loop logic), I'm going to have to down-vote. – Andreas Jul 02 '20 at 07:35
0

For the D grade, the if condition should be else if (marks == 2). You have put 1 instead of 2

Zico
  • 2,185
  • 1
  • 20
  • 23
Ridwan
  • 151
  • 1
  • 12
  • This answer does not help resolve OPs issue with the Scanner, it's rather a side comment. – Alex Rudenko Jul 02 '20 at 07:29
  • Although true, this doesn't answer the question about what causes the `NoSuchElementException`, so no up-vote from me. This should be a comment to the question, but you don't have enought rep yet for that (have 32, need 50), so the answer should stay, but don't expect up-votes. – Andreas Jul 02 '20 at 07:30
  • yeah i know, but unfortunately i do not have enough reputation to add this as a comment. – Ridwan Jul 02 '20 at 07:32
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/26567941) – Clijsters Jul 02 '20 at 08:36
0

I ran your code in editor fixing some minor issues in the loop and number of students check it worked fine with the input :

5
2 3 4 5 6

Here's the whole code block:

  import java.util.Scanner;

  public class HelloWorld{

     public static void main(String []args){
        
        Scanner scan = new Scanner(System.in);
    
        if(scan.hasNextInt() ){
           
        
            int numStudents = scan.nextInt();
            int marks;
        
            int gradeA = 0;
            int gradeB = 0;
            int gradeC = 0;
            int gradeD = 0;
        
            for (int i = 0; i < numStudents; i++){
                marks = scan.nextInt();
                if(marks == 5){
                    gradeA++;
                } else if (marks == 4){
                    gradeB++;
                } else if (marks == 3){
                    gradeC++;
                } else if (marks == 1){
                    gradeD++;
                }
            }
        
            System.out.println(gradeD);
            System.out.println(gradeC);
            System.out.println(gradeB);
            System.out.println(gradeA);
        
            }
       }
   }

This is the output in this case:

0
1
1
1
JimmyFlash
  • 775
  • 9
  • 18