Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Dont know whats wrong?

+4 votes
asked Nov 7, 2020 by Lee Aluague (160 points)
import java.util.Scanner;
class Main
{ public static void main(String[] args)
{ System.out.println("How much dirt is there in a hole 3 ft deep, 6 ft long and 4 ft wide? ");
Scanner input = new Scanner(System.in);
String ans;
ans = "None";
do{ int question,
String answer;
answer = "None";
question = input.nextInt();
if (question == answer)
{ System.out.println("Correct! or else it wouldn't be hole.");
break; }
else { System.out.println("Wrong, try again."); } }
while (ans.equals("None"));
    }
    
}

1 Answer

0 votes
answered Nov 7, 2020 by xDELLx (10,500 points)

original code:

int question,
String answer;


Corrected code:

int question, answer;

question = input.nextInt ();

 if (question == 3*6*4)  { ... }


Updated code below:

  import java.util.Scanner;
  class Main
  {
    public static void main (String[]args)
    {
      System.out.println
      ("How much dirt is there in a hole 3 ft deep, 6 ft long and 4 ft wide? ");
      Scanner input = new Scanner (System.in);
      String ans;
        ans = "None";
      do
        {
          int question, answer;
            question = input.nextInt ();
          if (question == 3*6*4)
            {
              System.out.println ("Correct! or else it wouldn't be hole.");
              break;
            }
          else
            {
              System.out.println ("Wrong, try again.");
            }
        }
      while (ans.equals ("None"));
    } 
    
  }


commented Nov 8, 2020 by Lee Aluague (160 points)
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.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Main.main(try.java:14)

I still get an error code when entering the correct answer.
commented Nov 8, 2020 by xDELLx (10,500 points)
Below is a sample run ,where I enter integers only.

How much dirt is there in a hole 3 ft deep, 6 ft long and 4 ft wide?                                                            
5                                                                                                                               
Wrong, try again.                                                                                                               
72                                                                                                                              
Correct! or else it wouldn't be hole.                                                                                           
                                                                                                                                
                                                                                                                                
...Program finished with exit code 0                                                                                            
Press ENTER to exit console.

------------------------------------------------------------------------
If normal ints are given ithe input the prog works fine ,for string the code throws exceptions.Using try-catch blocks should take care of that.

But I am not sure if the above working is what u expected.
Perhaps share a few sample cases with  input & the expected output.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...