Home > database >  Why is this answer correct on sololearn but... throws and exception error on a Java online IDE
Why is this answer correct on sololearn but... throws and exception error on a Java online IDE

Time:09-28

I got the answer correct, but during practice and IDE led me to believe the answer was wrong

the question was...

Tom and Bob are playing a board game, in which both players start with the same number of points. Tom won the first game and got 1 point, while Bob lost the game, and therefore lost 1 point.

You are given a program that is intended to take the initial score and increase Tom's score by 1 and decrease Bob's score by 1. But something is wrong: the program outputs the scores without the change.

Task Fix the program to result in the expected outputs.

the code in its original state...

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
       
       Scanner scanner = new Scanner(System.in);
       //taking initial score
       int initScore = scanner.nextInt();
       int scoreTom = initScore;
       int scoreBob = initScore;
       
       System.out.println("Round 1 results:");
       //fix
       System.out.println(scoreTom  );
       System.out.println(scoreBob--);
   }
} 

the correct answer is.. ( and this is what I exactly entered in sololearn)

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
       
       Scanner scanner = new Scanner(System.in);
       //taking initial score
       int initScore = scanner.nextInt();
       int scoreTom = initScore;
       int scoreBob = initScore;
       
       System.out.println("Round 1 results:");
       //fix
       System.out.println(  scoreTom);
       System.out.println(--scoreBob);
   }
}

the Online compiler I used was www.jdoodle.com

Why is this wrong on the online IDE but correct on sololearn.

Update on jdoodle i get:

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:9)

CodePudding user response:

It gave you NoSuchElementException because you didn't run your program in interactive mode. Hence, when you called scanner.nextInt() there was no input available.

The fix is to either slide the "Interactive" button to the right to enable it, or to enter the input in the right-hand text box before you run the program.

CodePudding user response:

Hey I am not sure why on sololearn it show you its correct but I will explain to you why its not.

on the right to the variable means to do the addition operation AFTER the statement is executed. On the left will happen BEFORE the statement is executed.

You can read further over here

Anyway what you have done is your code is:

Display the score -> increment the variable by one.

The correct code you added does: Increment the score by one -> Display the score

CodePudding user response:

Thank you everyone for responding. the issue was I was using the Online IDE incorrectly, I was unaware of the interactive button that jnoodle has. after pressing that button and then pressing run, I was able to get the program to build/compile. thank you everyone for taking the timeout your day to help me. if you got to jnoodles website copy and paste my code and do what I did not do was press the interactive button. and then run. I also did not see the "Stdin Inputs" section where I could had just entered a number and hit run with out turning on interactive mode as an alternative.

  •  Tags:  
  • java
  • Related