简体   繁体   中英

How do I have a program create its own variables with Java?

I would like to start off by saying that if this is common knowledge, please forgive me and have patience. I am somewhat new to Java. I am trying to write a program that will store many values of variables in a sort of buffer. I was wondering if there was a way to have the program "create" its own variables, and assign them to values. Here is an Example of what I am trying to avoid:

package test;

import java.util.Scanner;

public class Main {
     public static void main(String args[]) {

        int inputCacheNumber = 0;
        //Text File:
        String userInputCache1 = null;
        String userInputCache2 = null;
        String userInputCache3 = null;
        String userInputCache4 = null;

        //Program
        while (true) {
            Scanner scan = new Scanner(System.in);
            System.out.println("User Input: ");
            String userInput;
            userInput = scan.nextLine();

            // This would be in the text file
            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++;
            }
            // And so on

        }

    }
}

So just to try to summarize, I would like to know if there is a way for a program to set an unlimited number of user input values to String values. I am wondering if there is a way I can avoid predefining all the variables it may need. Thanks for reading, and your patience and help! ~Rane

You can use Array List data structure.

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

For example:

List<String> userInputCache = new ArrayList<>();

and when you want to add each input into your array like

if (inputCacheNumber == 0) {
    userInputCache.add(userInput); // <----- here
    inputCacheNumber++;
}

If you want to traverse your array list you can do as follows:

for (int i = 0; i < userInputCache.size(); i++) {
    System.out.println(" your user input is " + userInputCache.get(i));
} 

or you can use enhanced for loop

for(String st : userInputCache) {
    System.out.println("Your user input is " + st);
}

Note: it is better to put your Scanner in your try catch block with resource so you will not be worried if it is close or not at the end.

For example:

try(Scanner input = new Scanner(System.in)) {
    /*
    **whatever code you have you put here**
    Good point for MadProgrammer:
    Just beware of it, that's all. A lot of people have multiple stages in their   
    programs which may require them to create a new Scanner AFTER the try-block
    */
 } catch(Exception e) {
 }

For more info on ArrayList

http://www.tutorialspoint.com/java/java_arraylist_class.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM