0

My program seems to be working fine except that when it is supposed to loop back at the part asking user for operator the message shows but the is no room for user to input operator and it jumps directly to ask for first number, I do not know what I am doing wrong or not doing. I need help, please!

here is what I have so far:

import java.util.Scanner;

public class LabTresFinal
{

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

    //Declared Variables
    String operator;
    double more;
    double number1;
    double number2;
    double ans;
    boolean flag= true;

    do
     {
      //Prompt user for operation and numbers
      //Had to prompt for operation first otherwise my program was not runing correctly

      System.out.println( "Please choose an operation to be performed: + , - , * , / , or % .");
      operator = Keyboard.nextLine();
      System.out.println("Enter first number");
      number1 = Keyboard.nextDouble ();
      System.out.println("Enter second number");
      number2 = Keyboard.nextDouble ();

      switch (operator)
        {
          case ("+"):
          ans = number1 + number2;
          System.out.println(number1 + " + " + number2 + " = " + ans);

          break;

          case ("-"):
          ans = number1 - number2;
          System.out.println(number1 + " - " + number2 + " = " + ans);

          break;

          case ("*"):
          ans = number1 * number2;
          System.out.println(number1 + " * " + number2 + " = " + ans);

          break;

          case ("/"):

          if (number2 >= 1)
           {
             ans = number1 / number2;
             System.out.println(number1 + " / " + number2 + " = " + ans);

           }else if (number2 ==0)
           {
             System.out.println("Error: Operation cannot be performed with second number as zero, please try again");
             while(flag)
              {
                System.out.println("Enter second number");
                number2 = Keyboard.nextDouble ();
                if(number2>=1)
                 {
                   ans = number1 / number2;
                   System.out.println(number1 + " / " + number2 + " = " + ans);
                   flag=false;
                 }else if(number2 ==0)
                   {
                     System.out.println("Error: Operation cannot be performed with second number as zero, please try again");
                   }
              }
       }

     break;

     case ("%"):
     ans = number1 % number2;
     System.out.println(number1 + " % " + number2 + " = " + ans);

     break;
      }

  //setting loop for program to restart if user wants to
  System.out.println("Again?" 
                       + "\n" + "enter 1 for yes"
                       + "\n" + "enter 2 for no");
  more = Keyboard.nextDouble();
}while (more== 1);

} }

this is what my program is supposed to do :

It must:
- CLI based - Keep running totals for each operator (How many additions, how many multiplications, …)
- Prompt user for 2 numbers
- Prompt user for operator
- Handle the case of divide by zero - Perform the operation
- Display the result on the screen to the user
- Loop until the user uses a sentinel value
- Display statistics: count for each operation, minimum value, maximum value.

I know i still need to work a lot on it but i just need help on solving this problem for now so i can work on the rest.

Thank you so much.

  • @Manzanita A work around would be to provide the users with a numeric list corresponding to the operators and ask them to select a number – Brian Mar 06 '14 at 00:12
  • also there is another bug which will block you `more== 1` you are comparing double with `int` Use [`Double.compare()`](http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#compare%28double,%20double%29) – jmj Mar 06 '14 at 00:13
  • Duplicate of http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Jason C Mar 06 '14 at 00:14

2 Answers2

1

This is because, while nextDouble() skips newlines and whitespace, it does not consume newlines after a token is parsed (only before). Therefore, the newline is left on the stream, and the next call to nextLine() grabs that (blank line) immediately.

One option to handle this cleanly is to always use nextLine(), then explicitly parse the string into a Double (e.g. with Double.valueOf).

Other options are given as answers to Scanner issue when using nextLine after nextXXX.

By the way, traditional Java naming convention is to start variable names with a lowercase letter. As an example of the potential for confusion, personally the first time I read through your code I thought you were using a class named Keyboard with static methods (it also confused StackOverflow's syntax highlighter).

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
1

Instead of using:

Keyboard.nextLine();

Try:

Keyboard.next();

This should not skip the input request and you should be able to enter a new operator.