0

today I need some help with adding 4 user defined string, 2 int and 4 double variables into an arraylist as one single index.

Eg: (I'll cut down some of the variables for you, and keep in mind that sc is my scanner object and test results are out of 100)

System.out.print("enter your name")
String name = sc.next();
System.out.print("enter your test results")
double results = sc.nextDouble();
System.out.print("enter your mobile number")
int mobile_num = sc.nextInt();  //I may change this to a string

Now from here, I want to capture these inputs and store them in the array under the number 1 or the user's name. I also want to know how to use this same array to store unlimited inputs from the user through the code mentioned above.

Thanks in advance!

1 Answer 1

7

Create your own class UserInput which keeps 10 fields and then generalise a List with this type:

List<UserInput> list = new ArrayList<>();
list.add(new UserInput(name, results, mobile_num, ...));

If you want to associate a user's name with his corresponding input, you had better consider a Map<String, UserInput>:

Map<String, UserInput> map = new HashMap<>();
map.put(name, new UserInput(results, mobile_num, ...));
Sign up to request clarification or add additional context in comments.

2 Comments

but if I create a public class for UserInput, I'll receive alot of errors
@BzeQ, read the tutorial first

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.