#Scanner class not accepting input

1 messages · Page 1 of 1 (latest)

thorny sleet
#

`import java.util.Scanner;

public class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);

    System.out.println("Welcome to calculator");
    System.out.println("Enter first number:  ");
    int first_number = scanner.nextInt();
    System.out.println("Enter the operator:  ");
    String operator = scanner.nextLine();
    System.out.println("Enter the second number:  ");
    int second_number = scanner.nextInt();

    if (operator == "*") {
        System.out.println("The result of the arithmetic operation is :  " + first_number*second_number );
    }
    `

this is the code

deft gulchBOT
#

<@&987246399047479336> please have a look, thanks.

thorny sleet
#

the cursor in present but not accepting input

#

vscode is the IDE

deft gulchBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

pallid river
#

read this

#

it explains kinda what happens behind the scenes and why mixing nextInt and nextLine is not a great idea if you dont know what you do

thorny sleet
#

alright

#

let me try doing it the other way

pallid river
#

also you shouldnt compare strings with ==

#

instead use .equals

thorny sleet
#

it still isnt taking the input

#

`import java.util.Scanner;

public class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);

    System.out.println("Welcome to calculator");
    System.out.println("Enter first number:  ");
    int first_number = Integer.parseInt(scanner.nextLine());
    System.out.println("Enter the operator:  ");
    String operator = scanner.nextLine();
    System.out.println("Enter the second number:  ");
    int second_number = Integer.parseInt(scanner.nextLine());

    if (operator.equals("*")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number*second_number );
    }

`

#

stuck here

pallid river
#

you actually tried typing the number?

thorny sleet
#

yeah

#

oh wait

#

its says this a read only editor

#

how do i make it editable

pallid river
#

uh idk

#

which conoole are you using?

thorny sleet
#

output

pallid river
#

its a vsc issue:

thorny sleet
#

ok thanks

pallid river
#

I generally recommend other ides for java, like intellij or eclipse

thorny sleet
#

`import java.util.Scanner;

public class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);

    System.out.println("Welcome to calculator");
    System.out.println("Enter first number:  ");
    int first_number = Integer.parseInt(scanner.nextLine());
    System.out.println("Enter the operator:  ");
    String operator = scanner.nextLine();
    System.out.println("Enter the second number:  ");
    int second_number = Integer.parseInt(scanner.nextLine());

    if (operator.equals("*")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number*second_number );
        
    else if (operator.equals("/")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number/second_number );
    }
    
    else if (operator.equals("+")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number + second_number);
        
    else if (operator.equals("-")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number - second_number);
        
    else{
        System.out.println("Program failed to run");
    }
        
    }
        
    }
    }
}

}`

#

***ERROR!
/tmp/wNHNhQckKu/Main.java:29: error: 'else' without 'if'
else{
^
ERROR!
/tmp/wNHNhQckKu/Main.java:26: error: 'else' without 'if'
else if (operator.equals("-")) {
^
ERROR!
/tmp/wNHNhQckKu/Main.java:19: error: 'else' without 'if'
else if (operator.equals("/")) {
^
3 errors

=== Code Exited With Errors ===***

pallid river
#
if (operator.equals("*")) {
    System.out.println("The result of the arithmetic operation is :  " + first_number*second_number );    
} else if (operator.equals("/")) {
    System.out.println("The result of the arithmetic operation is :  " + first_number/second_number );
}

there needs to be a closing } before the else if

thorny sleet
#

is it necessary?

#

i just shofted from python so the synta is going over my head

pallid river
#

yes it is necessary

#

well there are cases where its not needed

torn mica
#

Without only the first line is part of the if or else. But it's recommended to always use { ... } .

pallid river
#

but everything in a { } is in a kind of group

#

in python you indicate that using indentation for if

thorny sleet
#

`import java.util.Scanner;

public class Main {

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);

    System.out.println("Welcome to calculator");
    System.out.println("Enter first number:  ");
    int first_number = Integer.parseInt(scanner.nextLine());
    System.out.println("Enter the operator:  ");
    String operator = scanner.nextLine();
    System.out.println("Enter the second number:  ");
    int second_number = Integer.parseInt(scanner.nextLine());

    if (operator.equals("*")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number*second_number );
        
    }else if (operator.equals("/")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number/second_number );
    
    
    }else if (operator.equals("+")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number + second_number);
        
    }else if (operator.equals("-")) {
        System.out.println("The result of the arithmetic operation is :  " + first_number - second_number);
        
    }else{
        System.out.println("Program failed to run");
    }
}

}`


ERROR!
/tmp/UsuW5kfcn5/Main.java:27: error: bad operand types for binary operator '-'
System.out.println("The result of the arithmetic operation is : " + first_number - second_number);
^
first type: String
second type: int
1 error

=== Code Exited With Errors ===***

#

why am i getting this error only in the subtraction part

pallid river
#

you would want this:

"The result of the arithmetic operation is :  " + (first_number - second_number)
#

without it you wont get the expected results or error in the case of -

#

otherwise it is going to evaluate that line from left to right

#

and it starts with:

// String + int = String
"The result of the arithmetic operation is :  " + first_number

and then lets say first_number = 2, it will continue with the second number:

"The result of the arithmetic operation is :  2" + second_number

and if for example second_number = 4, you going to get this result:

"The result of the arithmetic operation is :  24"
#

you get an error for - because you cant do String - int

thorny sleet
#

hmm

#

i get it now

#

i will change the code now

#

thanks a ton

#

how do i close this thread

pallid river
#

/help-thread close peepo_heart