I/o test failure - School assignment

Question:
I get a failure of run test, where do i have mistake?
Repl link:
https://replit.com/@EuroTech-SDET9/31-School-Gradesscanner-YAVUZ7


// Enter import
import  java.util.Scanner;

class Main {
  public static void main(String[] args) {
  // Enter your code here
    String  subject1;
    String  subject2;
    String  subject3;
    String  subject4;
    String  subject5;
    String  summary;
    double  grade1;
    double  grade2;
    double  grade3;
    double  grade4;
    double  grade5;
    double  average;    
    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to the Grader!");
    System.out.println("Please enter subject name number 1 and score for this subject");
    subject1 = scan.nextLine();
    grade1 = scan.nextDouble();
    System.out.println("Please enter subject name number 2 and score for this subject");
    subject2 = scan.nextLine();
    grade2 = scan.nextDouble();
    System.out.println("Please enter subject name number 3 and score for this subject");
    subject3 = scan.nextLine();
    grade3 = scan.nextDouble();
    System.out.println("Please enter subject name number 4 and score for this subject");
    subject4 = scan.nextLine();
    grade4 = scan.nextDouble();
    System.out.println("Please enter subject name number 5 and score for this subject");
    subject5 = scan.nextLine();
    grade5 = scan.nextDouble();
    summary = "Summary: " + subject1 + " - " + grade1 + ", " + subject2 + " - " + grade2 + ", " + subject3 + " - " + grade3 + ", " + subject4 + " - " + grade4 + ", " + subject5 + " - " + grade5;
    average = (grade1 + grade2 + grade3 + grade4 + grade5)/5;
    System.out.println(summary);
    System.out.println("Your average score is: " + average);
    System.out.println("Thank you for using Grader!");
    System.out.println("Goodbye!");
    

       

           
  }
}

Is this a school assignment? feels that way so i edited the title, if i am wrong change the title again.
As for error … i see that there is a public missing very very early …

yes this is a school assignment and i am new at replit.Btw when i run test i get this error

Exception in thread “main” java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Main.main(Main.java:27)

Welcome to the forums, @YAVUZ7!

You should not be posting your school/uni assignments to this forum. It’s not right to expect others to do the work for you and your tutor will not understand how to help you if you are submitting work from someone else. It is against our policy here.

Please feel free to post questions that will help you understand the building blocks of programs, but please don’t post tasks.

1 Like

oh sorry i am new here and about to learn everything one by one, have a nice day

Isn’t if allowed if we tell them where the mistake is, but don’t fix it?

2 Likes

Well, we can give them hints to help them figure it out.

2 Likes

Isnt telling where the mistake is, but not fixing it doing that?

2 Likes

Yeah, I think so. Just showing where the problem is.

We just need to give some hints. No more than that

okay @YAVUZ7 , pay attention the the scanner input, what could be causing the error.
what is the name of the error and what does it mean.

Honestly this would be a really hard problem to solve if you don’t know what’s going on. I will explain the problem, and you can see if you can fix it.

The error you’re seeing is because you’re typing in text when your program is looking for a double. Here’s why…

In Java, the scanner functions that read numeric datatypes (in your case nextDouble) behave a little bit differently than nextLine() does.
When nextLine() is called, it reads everything from the input buffer up until the newline character (the character that represents an enter press) and puts it in your String. nextLine() will then discard the newline character from the input buffer.
nextDouble however, behaves differently. nextDouble will attempt to read a double from the input buffer. After doing so, It LEAVES the newline character (the enter press) in the buffer though!
So that means that next time you call newLine(), it will grab that enter press (newline character) that’s still in the buffer, and throw it away. It doesn’t actually ask you to enter any text, because it thinks you already have! So your String will actually contain nothing.
So when nextDouble() gets called again, you think you’re supposed to type in text, and it tries to read that text. But nextDouble can’t understand text, so the program crashes.

Let me illustrate this.
You call nextLine(). The buffer is empty. The program waits for you to type in text. You type in “math\n” (\n represents your enter press).
Your program reads in the text “math” and throws away the \n. The buffer is now empty.
Then you call nextDouble(). The buffer is empty. The program waits for you to type in a double. You type “3.5\n”.
Your program reads in the double “3.5” and LEAVES THE \n IN THE BUFFER. The buffer is NOT empty.
You call nextLine() again. Because the buffer is not empty, the program does not wait for you to type anything in, and grabs the \n from the buffer and discards it. The buffer is now empty.
You then call nextDouble(). The buffer is empty. The program waits for you to type in text. You THINK you are supposed to type in text, so you enter something like “science\n”.
Your program attempts to read a double from the buffer and encounters text. Your program now crashes with the error you’re seeing.

So that’s the explanation of the error, hopefully you can figure out how to fix it. :slight_smile:

4 Likes

Yeah, that is what i was going for in my post. idk if this is too much of a hint but… wow that must took a long time to type. Thanks for the explanation for this. As a java coder the reason as to why this doesn’t work has eluded me.