3

I am very new to Java, (and to programming altogether). I am sure that the solution to my problem is very simple, but I just can't figure it out. The code below is a small chunk of my program. But it is still compile-able, and still has the same problem.

Here is the code::

import java.util.Scanner;

public class Practice{
    public static void main(String args[]){

        Scanner input = new Scanner(System.in);

        String command = "";

        double d; // Distance
        double t; // Time
        double s; // Speed

        System.out.println("Hello, I am the Physics Calculator!!");

        while (!command.equals("end")){

            System.out.print("What would you like to calculate?: "); // after the first loop, this statement is printed twice to the screen! :(
            command = input.nextLine();
            System.out.println();

            if (command.equals("speed")){  

                System.out.print("What is the distance in meters?: ");
                d = input.nextDouble();

                System.out.print("What is the time is seconds?: ");
                t = input.nextDouble();

                s = d / t;

                System.out.println("The Speed is "+ s +" m/s");
                System.out.println();

            }
        } //End of the while-loop
    }
}

The first line of code within the while-loop is:

System.out.println("What would you like to calculate?: ");

So far so good. When I run the program, it prints: *What would you like to calculate?: * And then the program continues as expected.

The problem is after the program reaches the end of the while-loop, and returns to the top of the while loop. it will print:

What would you like to calculate?:
What would you like to calculate?:

I just can't figure out why it prints it out twice.

Here is an example of the exact output I received when running the program (The italic is the output to the console and the bold is my input):

Start{

Hello, I am the Physics Calculator!!
What would you like to calculate?: speed

What is the distance in meters?: 50
What is the time is seconds?: 50
The Speed is 1.0 m/s

What would you like to calculate?:
What would you like to calculate?:
}End

At the end, I can still type 'speed' and continue on with the program. I just want to get rid of the second 'What would you like to calculate'.

Any input regarding the problem that I am having will be highly appreciated!!

Thank you very much for your time!!

  • 2
    Please consider the coding conventions of Java as well. For naming you should be using `input` rather than `Input` and `command` instead of `Command`. – Ean V Feb 24 '14 at 04:53
  • Thank you Ean!! I did some research on the coding conventions of Java, and you are right! I was not following the conventions, so I edited my post, (and my program), and corrected my mistakes. Thanks again! – JavaSufferer Feb 24 '14 at 05:59

1 Answers1

4

This happens because nextDouble doesn't consume the newline that follows the number. You need to do that separately. Currently, that newline (after the number) gets consumed the next time you are prompted for a command. Since empty string is not "speed", the loop goes through one extra time.

Add Input.nextLine(); after t = Input.nextDouble(); and this will work just fine.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Thank you David!! When I did as you suggested, it fixed the problem!! Although I have to admit, I don't understand why adding `Input.nextLine();` fixed the problem. But I am very new to programming, (only two weeks in), and I am sure that I will figure it out as I learn more. Thanks again!! – JavaSufferer Feb 24 '14 at 05:34
  • @JavaSufferer When you press 'enter' to submit, it types a new line character in to System.in. When you call nextDouble, Scanner reads some amount of characters that are the next numerical string it finds in the input, leaving anything else leftover. Calling nextLine reads the next line from the stream which in this case was just the new line character from the last entry that nextDouble did not read. It's just a weird interaction between Scanner and System.in. – Radiodef Feb 24 '14 at 06:32
  • Yes, @Radiodef, that's a good way of expressing it. There are many, many questions on this site about this exact same problem - with programs that use `nextLine` and either `nextInt` or `nextDouble` on the same `Scanner`. Unfortunately, if you don't know that this is what the problem is, it's hard to know what to search for. JavaSufferer, I would recommend doing a quick search for something like "Scanner nextLine" and reading some of the answers to these other questions. – Dawood ibn Kareem Feb 24 '14 at 06:36