简体   繁体   中英

How to stop scanner from accepting input

I'm working on very simple code which asks you to enter how much money you have and and which products you wish to buy (on one line). The program is then supposed to tell you whether you have enough money to buy the products or not. Also, it should print the product with the lowest price.

Example:

  1. Enter amount of money you have: 100
  2. Enter products you want to buy and the value for each: Apple 10 and Orange 20

  3. Output of the code:

    • you have enough money
    • the lowest price is (Apple 10)

I have 2 problems with this code

  1. First, when I try to stop the scanner from taking inputs I'm supposed to enter "stop" as an input. However, in my case the action is only performed only if I enter "stop" 2 times. I don't know why.

  2. I need to determine the minimum product value and print it. I have tried a lot of different things, but none of them worked for me.

This is my code so far:

Scanner input = new Scanner (System.in);

String productName="";
double totalPrice=0;
double productValue = 0;


System.out.println("How much money do you have? ");
double money = input.nextDouble();

System.out.println("please insert the items in the invoice (the name of product and its price): "
        + " insert \"stop\" as the name of the product to finish your input");


while (!(productName.equals("stop")) ){
    if(input.hasNext()){  productName = input.next();}
    if (input.hasNextDouble()){ productValue = input.nextDouble();}
    totalPrice = totalPrice + productValue;
}

if   (money > totalPrice ){    
    System.out.println("you have enough money");
} else {
    System.out.println("you don't have enough money");
}

Instead of parsing the user input the way you are now, try parsing the entire line at once and then splitting up the input string using String.split()

Also consider what the your first call to Scanner.nextDouble() is really doing. It will read the next double input by the user but will not read to the next line (won't read past the newline character)

Your code is reading two items before checking to see if the user wants to stop and that is why you're having to provide two inputs. To determine the minimum value just keep track of the lowest value you've seen so far along with the name associated with that value.

    Scanner input = new Scanner (System.in);

    String productName="", minProductName = null;
    double totalPrice=0;
    double productValue = 0, minValue = -1;


    System.out.println("How much money do you have? ");
    double money = input.nextDouble();

    System.out.println("please insert the items in the invoice (the name of product and its price): "
            + " insert \"stop\" as the name of the product to finish your input");


    while (true){
        productName = input.next();
        if("stop".equals(productName))
            break;
        productValue = input.nextDouble();
        if(minValue < 0 || minValue > productValue){
            minValue = productValue;
            minProductName = productName;
        }
        totalPrice = totalPrice + productValue;
    }

    if   (money > totalPrice ){    
        System.out.println("you have enough money");
    } else {
        System.out.println("you don't have enough money");
    }

    System.out.println("Minimum product value: "+minProductName + " " +minValue);

    input.close();

Input/Output:

How much money do you have? 
100
please insert the items in the invoice (the name of product and its price):  insert "stop" as the name of the product to finish your input
apple 10
orange 5
banana 50
stop
you have enough money
Minimum product value: orange 5.0

Considerations/Notes:

You may notice that this condition has been flipped:

if("stop".equals(productName))

This is intentional because if you have a null productName somehow then your code will throw a null pointer if you use productName.equals(...) but if you use a constant like "stop" there is no way this can be null so it will never throw a NullPointerException .

You never validate your input - what if the user enters something that is less than zero for the value? Is that valid? If not then what should happen?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM