Hi I am totally new in Java and don't know much about it .I Have just made a program and not sure if it's correct.I want a input from user so that it will calculate the answer.Here's the code:
import java.util.Scanner;
class Vedant
{
public static void main(String[] args)
{
Scanner inputa=new Scanner(System.in);
int b=inputa.nextInt();
if(b<20);
int a=5;
{
System.out.println("Answer ="+a);
}
}
}
When I run this file in cmd it does nothing.It doesn't even ask for input.If I type random things and press enter it gives me this
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Vedant.main(Vedant.java:8)
Please help.And yeah I am just a beginner
When I run this file in cmd it does nothing.
That's incorrect
It doesn't even ask for input
You didn't tell it to, the command you gave is inputa.nextInt()
which only reads input, but doesn't output anything.
If I type random things and press enter it gives me this
inputa.nextInt()
means "read the next input as a number". If you're typing random things it can't convert it to a number.
There are some more issues with your code:
if(b<20);
The semicolon at the end means that nothing is done if b
actually is less than 20
int a=5;
{
System.out.println("Answer ="+a);
}
The parentheses are not needed here, and because you explicitly set the variable a
to 5 your application, if it gets this far, will always say Answer =5