I would like to start with the fact that, if this is well known, please forgive me and show patience. I am a little new to Java. I am trying to write a program that will store many values of variables in the form of a buffer. I was wondering if there is a way for the program to "create" its own variables and assign them to values. Here is an example of what I'm trying to avoid:
package test;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int inputCacheNumber = 0;
String userInputCache1 = null;
String userInputCache2 = null;
String userInputCache3 = null;
String userInputCache4 = null;
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("User Input: ");
String userInput;
userInput = scan.nextLine();
if (inputCacheNumber == 0) {
userInputCache1 = userInput;
inputCacheNumber++;
System.out.println(userInputCache1);
} else if (inputCacheNumber == 1) {
userInputCache2 = userInput;
inputCacheNumber++;
} else if (inputCacheNumber == 2) {
userInputCache3 = userInput;
inputCacheNumber++;
} else if (inputCacheNumber == 3) {
userInputCache4 = userInput;
inputCacheNumber++;
}
}
}
}
Therefore, to try to generalize, I would like to know if there is a way for a program to set an unlimited number of user input values for String values. I am wondering if there is a way to avoid predefining all the variables that you might need. Thanks for reading, and your patience and help! ~ Rane