BzeQ
BzeQ

Reputation: 73

How to add multiple variables as a single value in an arraylist?

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!

Upvotes: 0

Views: 1686

Answers (1)

Andrew
Andrew

Reputation: 49656

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, ...));

Upvotes: 7

Related Questions