pgray10
pgray10

Reputation: 143

String input of more than one word causes exception error

I am inputting the following things:

For some reason, the prompt for cost comes up and the next output line comes on the same line. Could someone help me to spot my mistake?

public class SalesTax {

    public static void main(String[] args) {
        // Input items for shopping cart
        HashMap<String, String> cart = new HashMap<String, String>();

        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // variables
        char done;
        boolean goods;
        double tax;

        // Pick items for list.
        do {       
            System.out.print("Please enter an item.");
            String item = input.next();

            System.out.print("Please enter the price for "+ item + ": ");
            String price = input.next();      

            if (item.contains("book")) {
                goods = false;  
            } else if(item.contains("chocolate")) {
                goods = false;
            } else if(item.contains("pill")) {
                goods = false;
            }

            cart.put(item, price);
            System.out.print("Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.");
            done = input.next().charAt(0);

        } while(Character.toUpperCase(done) == 'Y');
    }
}

Upvotes: 0

Views: 1750

Answers (4)

smali
smali

Reputation: 4805

you are using System.out.print() instead use System.out.println();

print() will just print the word and stays on the same line.

println() will print the whole line and cursor goes to second line .

And don't use the spaces while reading as you are writing it as

input.next();

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.

edit: just change the declaration to this.

 Scanner s = new Scanner(input).useDelimiter("\n");

It will change the delimiter to new line and read the complete line.

Upvotes: 0

Mahder
Mahder

Reputation: 419

use input.nextLine() to read an entire line from the keyboard. Means what ever the user typed till the user presses the enter key.

One thing that I did not understand is, what is the use of having this in your code

        if(item.contains("book"))
        {
            goods = false;  
        }
        else if(item.contains("chocolate"))
        {
            goods = false;
        }
        else if(item.contains("pill"))
        {
            goods = false;
        }

??? Can you please explain?

Thanks,

Upvotes: 0

Nischaal Cooray
Nischaal Cooray

Reputation: 210

By default, next() just gets input upto a whitespace so you'd have to use nextLine() instead, which will read in the whole line of input upto a carriage return.

Upvotes: 0

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

problem:

String item = input.next();

By the time you input music cd it will consume music by item and price will consume the cd thus skipping it.

solution:

you need to call input.nextLine(); to consume the whole line of string

Upvotes: 2

Related Questions