0

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");
    }
   }
 }
Laurel
  • 5,522
  • 11
  • 26
  • 49
ExpertNoob
  • 139
  • 8
  • You should either use nextLine throughout (and parse input to integer when necessary) or fire a blank nextLine before the required nextLine. Check this thread for more explanation: https://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Olantobi Jul 03 '16 at 17:30

1 Answers1

0

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
Tom Njigi
  • 148
  • 3
  • Thanks for the help, your solution worked. I read its documentation but as I'm new to java, I have one more question, would my program have worked , If I would have used "print" instead of "println". Because then ,Is think there would be no newline character for nextLine(). Thanks once again. – ExpertNoob Jul 03 '16 at 17:44
  • Nope. print / println and nextLine are totally different. The print / println methods belong to the standard output stream `System.out` i.e. they are used to **display output** to the user. On the other hand nextLine method 'belongs' to the standard input stream `System.in` i.e. it's used to **get input** from the user. In your case the problem is with the `System.in` not `System.out`. [Read more here](https://docs.oracle.com/javase/7/docs/api/java/lang/System.html) – Tom Njigi Jul 03 '16 at 18:01