user2774067
user2774067

Reputation: 1

Scanner breaking after entering 2 words

I have a problem with the "model" scanner. My program requires me to enter vehicles from a predetermined chart. Some of the vehicles are two words example : "Grand Caravan". I understand why its giving me other then being two words its because the next scanner is a double which doesn't allow characters so it errors out. I just want to include the white space and 2nd word in a single scanner if possible, or is there another way of doing this?

String make, model;
    double cityMPG, hwyMPG;

    do {
        System.out.println("Please enter your cars make"); // asking for cars make
        make  = keyboard.next();

        System.out.println("Enter your cars model"); // asking for cars model
        model = keyboard.next();

        System.out.println("Enter your gas tank size"); // asking for gas tank size
        tanksize = keyboard.nextDouble();

        System.out.println("Enter your city MPG"); // asking for City MPG1
        cityMPG = keyboard.nextDouble();

        System.out.println("Enter your highway MPG"); // asking for Highway MPG
        hwyMPG = keyboard.nextDouble();

Upvotes: 0

Views: 55

Answers (2)

Johnson Wong
Johnson Wong

Reputation: 4483

Explanation in Scanner.nextLine() JavaDoc will explain what you want.

For the future, when you are faced with similar issues, look into the JavaDocs for the object you're using. Many times, just browsing through the methods will find you one that does what you need.

Upvotes: 0

Jsdodgers
Jsdodgers

Reputation: 5312

You can use keyboard.nextLine() instead of keyboard.next().

Upvotes: 5

Related Questions