ExpertNoob ExpertNoob - 5 months ago 9
Java Question

Not Able to Use the Scanner Object on changing the position where I create it

I just started learning java on my own and I'm facing a problem with this simple calculator program that I wrote.

In the first code, I get the expected output, if I input integers with

nextInt()
(operands for arithmetic operations) before taking the string input through
nextLine()
(the arithmetic operations, ADD, SUB, MUL).

That is, in total I take 3 inputs.

It checks for the conditions and operates according to the condition which is true.

But if I change the position, i.e., first take the
String
input (the operations) and then the integer input (the numbers), it does not take any input for the String (operations). It also doesn't check for any conditions.

That is, in this case, I am able to input only the 2 integers and not the operation string.

What's wrong with the code? I have run it in Eclipse and Netbeans and the result is same.

The one which gives right output

import java.util.Scanner;
public class Test
{
public static void main(String args[])
{

float fnum, snum, result;

Scanner input= new Scanner(System.in);

System.out.println("What Operation do you want to perform?");
System.out.println("Enter The Choice \n\"ADD\" -For Addition");
System.out.println("\"SUB\"- For Subtraction");
System.out.println("\"MUL\"- For Multiplication");
System.out.println("\"DIV\"- For Division");

/**/String a= input.nextLine(); // on changing its position in the 2nd one

System.out.print("Enter the First Number:");
fnum=input.nextInt();
System.out.println("The First Number Entered is : "+fnum);

System.out.print("Enter the Second Number:");
snum=input.nextInt();
System.out.println("The Second Number Entered is: "+snum);

if(a.equals("ADD"))

{
result= fnum+snum;
System.out.print("The Result After ADDITION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("SUB"))
{
result=fnum-snum;
System.out.print("The Result After SUBSTRACTION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("MUL"))
{
result=fnum*snum;
System.out.print("The Result After MULTIPLICATION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("DIV"))
{
result=fnum/snum;
System.out.print("The Result After DIVISION of the Two Numbers Is:");
System.out.println(result);
}

else
{
System.out.println("Invalid Case");
}
}
}


The Second one which has the problem

import java.util.Scanner;
public class Test
{
public static void main(String args[])
{
float fnum, snum, result;

Scanner input= new Scanner(System.in);


System.out.print("Enter the First Number:");
fnum=input.nextInt();
System.out.println("The First Number Entered is : "+fnum);

System.out.print("Enter the Second Number:");
snum=input.nextInt();
System.out.println("The Second Number Entered is: "+snum);

System.out.println("What Operation do you want to perform?");
System.out.println("Enter The Choice \n\"ADD\" -For Addition");
System.out.println("\"SUB\"- For Subtraction");
System.out.println("\"MUL\"- For Multiplication");
System.out.println("\"DIV\"- For Division");

/**/String a= input.nextLine();// on changing the place of this, goes directly to the last else case, takes no input


if(a.equals("ADD"))

{
result= fnum+snum;
System.out.print("The Result After ADDITION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("SUB"))
{
result=fnum-snum;
System.out.print("The Result After SUBSTRACTION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("MUL"))
{
result=fnum*snum;
System.out.print("The Result After MULTIPLICATION of the Two Numbers Is:");
System.out.println(result);
}

else if(a.equals("DIV"))
{
result=fnum/snum;
System.out.print("The Result After DIVISION of the Two Numbers Is:");
System.out.println(result);
}

else
{
System.out.println("Invalid Case");
}
}
}

Answer

Basically, you are still on the same line. so calling input.nextLine(); Advances this scanner past the current line and returns the input that was skipped.

Check the documentation for nextLine() method for a detailed explanation.

To achieve what you want you need to call nextLine twice. i.e.

input.nextLine();//to complete the current line
String a = input.nextLine();//To get the user input
Comments