0

I am using Eclipse and have a general programming question. I want to make a new variable everytime the user does something like press a button. If it was to make a variable once it would be straightforward ; in the code for pressing the button I would declare -

String title1 = “Title1”;

However, I want a new, different variable to be made everytime the user clicks the button. I tried different things like a for loop to modify the name of the next variable and all but ran out of ideas so came here to ask my first question.

Thank you all and apologies if this is a stupid question.

  • It's not a "stupid" question, but it is a common question. Consider doing a little searching on the subject first, since more discussion of the same thing will probably not add much to this site. – Hovercraft Full Of Eels Apr 29 '14 at 00:48
  • For example, here's one: possible duplicate of [Assigning variables with dynamic names in Java](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java). Voting to close this question as a duplicate. – Hovercraft Full Of Eels Apr 29 '14 at 00:49
  • I am not an experienced programmer. I thought I looked high and low for the answer but I guess I was searching for the wrong key words, like "generate" "new variable". If you feel this question should be deleted I would not contest. – user3583364 Apr 29 '14 at 19:59
  • @user3583364 Where the duplicate and its target use different keywords we like to keep both around as other people may search using the same terms – Richard Tingle Apr 29 '14 at 20:31

4 Answers4

1

What you are saying cannot be done. What you can do instead is use an ArrayList<String> to which you can add String on user clicks.

SSCCE:

import java.util.*;

public class ArrayListExamples {

    public static void main(String args[]) {
        // Creating an empty array list
        ArrayList<String> list = new ArrayList<String>();

        // Adding items to arrayList
        list.add("Item1");
        list.add("Item2");
        list.add(2, "Item3"); // it will add Item3 to the third position of
                                // array list
        list.add("Item4");

        // Display the contents of the array list
        System.out.println("The arraylist contains the following elements: "
                + list);

        // Checking index of an item
        int pos = list.indexOf("Item2");
        System.out.println("The index of Item2 is: " + pos);

        // Checking if array list is empty
        boolean check = list.isEmpty();
        System.out.println("Checking if the arraylist is empty: " + check);

        // Getting the size of the list
        int size = list.size();
        System.out.println("The size of the list is: " + size);

        // Checking if an element is included to the list
        boolean element = list.contains("Item5");
        System.out
                .println("Checking if the arraylist contains the object Item5: "
                        + element);

        // Getting the element in a specific position
        String item = list.get(0);
        System.out.println("The item is the index 0 is: " + item);

        // Retrieve elements from the arraylist

        // 1st way: loop using index and size list
        System.out
                .println("Retrieving items with loop using index and size list");
        for (int i = 0; i < list.size(); i++) {
            System.out.println("Index: " + i + " - Item: " + list.get(i));
        }

        // 2nd way:using foreach loop
        System.out.println("Retrieving items using foreach loop");
        for (String str : list) {
            System.out.println("Item is: " + str);
        }

        // 3rd way:using iterator
        // hasNext(): returns true if there are more elements
        // next(): returns the next element
        System.out.println("Retrieving items using iterator");
        for (Iterator<String> it = list.iterator(); it.hasNext();) {
            System.out.println("Item is: " + it.next());
        }

        // Replacing an element
        list.set(1, "NewItem");
        System.out.println("The arraylist after the replacement is: " + list);

        // Removing items
        // removing the item in index 0
        list.remove(0);

        // removing the first occurrence of item "Item3"
        list.remove("Item3");

        System.out.println("The final contents of the arraylist are: " + list);

        // Converting ArrayList to Array
        String[] simpleArray = list.toArray(new String[list.size()]);
        System.out.println("The array created after the conversion of our arraylist is: "
                        + Arrays.toString(simpleArray));
    }
}  

Source: http://examples.javacodegeeks.com/core-java/util/arraylist/arraylist-in-java-example-how-to-use-arraylist/

An SO User
  • 24,612
  • 35
  • 133
  • 221
0

Use an ArrayList<String> or a LinkedList<String>. An array-based collection can hold an indefinite number of objects (think of them as separate variables) accessed by numerical index.

List<String> titles = new ArrayList<String>();
titles.add("Title1");
titles.add("Title2");
. . .
System.out.println(titles.get(1));
System.out.println(titles.get(2));
. . .
System.out.println("There are " + titles.size() + " titles:");
for (String title : titles) {
    System.out.println(title);
}

If you want to access them by symbolic name instead of index, use a HashMap<String,String>.

Map<String,String> titles = new HashMap<String,String>();
titles.put("title1", "The First Title");
titles.put("title2", "The Second Title");
. . .
System.out.println(titles.get("title1"));
// etc.

The values can be any object type, not only String.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Use a Map instead of having individual variables, and you can add as many entries as you need.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

why you want to make a new variable every time a user click the button. I mean its wastage of resources. Moreover if you want to make your program optimized then use Map or List as suggested by above experts. Agreed with you guys.