0

This is the output I'm trying to create:

Please enter food servings separated by a blank in the order

Vegetables & Fruit, Grain Products, Milk & Alt, Meat & Alt: 8 7 1 3

Food Group Servings

Vegetables & Fruit 8

Grain Products 7

Milk & Alternatives 1

Meat & Alternatives 3

How can I accept multiple integers in the same line from user input, whilst assigning those integers to their own variables?

0

1 Answer 1

1

You can split the input by spaces and assign them to their own variables

String input = blah;
String[] split = input.split("\\s+");

int veg = 0;
int milk = 0;
//add more variables

veg = Integer.parseInt(split[0]);
milk = Integer.parseInt(split[1]);
//add more stuff here
Sign up to request clarification or add additional context in comments.

2 Comments

Lol, beat me to it :)
And what happens if the user only enters 2 values instead of 4 ? or enters :1 1w 4 8 ?

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.