Basmeuw Basmeuw - 1 year ago 46
Java Question

Is there a way to create a new variable from user input in Java

I want to make an user database, which creates a new variable from the string that was entered in the console. I don't know if this is possible and i've searched everywhere.

Sky Sky
Answer Source

You can use data structures like a List, which can hold a number of objects. A list grows when you add objects to them. A simple list to get started with is java.util.ArrayList.

Example to get you started:

 // create a new list which can hold String objects
 List<String> names = new ArrayList<>();
 String nextName = scanner.nextLine();

 // read names until the user types stop
 while(!nextName.equals("stop")) {
     // add new name to the list. note: the list grows automatically.
     // read next name from user input
     nextName = scanner.nextLine();

 // print out all names.
 for(String name : names) {