0

am new to learning java and I have set myself a task of creating a shopping basket.

Here is my code:

System.out.println("Grapes " + "£" + grapes + "    Quantity:");
    input= amount.nextLine();
    System.out.println("You Selected " + input + " Grapes");

How can i add a boolean so when someone says they want to order 1 bunch of grapes it comes up with "Bunch of Grapes", and when someone orders 2+ bunches of grapes it comes up with "Bunches of grapes" .

Thank you for you help,

Peter

2 Answers 2

1

You would do something like:

boolean multiple_grapes = (Integer.valueOf(input) > 1);
if (multiple_grapes) {
  System.out.println("You Selected " + input + " Bunches of Grapes");
} else {
  System.out.println("You Selected " + input + " Bunch of Grapes");
}

You need to parse input to Integer in order to be able to compare it with the integer value 1.

Sign up to request clarification or add additional context in comments.

Comments

1
int num = Integer.parseInt(input);
if(num == 1){
    // do whatever you want
}
else {
    // another action
}

Actually, you can even compare string representation without parsing

if(input.equals("1"))

but I guess you will need integer representation anyway

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.