Adding to string array

I have a java code that looks like this:

x = Math.random() * emotion.length;
Feeling = (int)x;
System.out.println(emotion[Feeling] + " How are you?");
NewEmotion1 = NewEmotion.nextLine();

Eclipse gives me and error that says “Cannot invoke add(String) on the array type String[]”. emotion is and Array. I am trying to add NewEmotion1 from the scanner to emotion. I initiated emotion like this:

String[] emotion = new String[]{"Ecstatic", "Meh.", "Bad.", "Good."};

I only want it to show one random emotion at a time.

I show the length of the array with this code:

String[] emotion = new String[]{"Ecstatic.", "Meh.", "Bad.", "Good."};

All help would be appreciated.


Initiating a dynamic list should be as the follow:

List<String> emotion = new ArrayList();

By the given error, I can assume you initiated the array in this way:

String[] emotion = new String[X]

where X represents the capacity of the array, which would be fixed size. By initiation an array in your case, using emotion[i] = YOUR_STRING, where 0 <= i < X will solve your problem.

as Federico klez Culloca suggest, using lowcase letter for variables is more intuitive.


Since each time you want to add a new emotion to the list, this example will help you understand the flow:

    Scanner io = new Scanner(;
    List<String> emotions = new ArrayList<>(Arrays.asList("Ecstatic", "Meh.", "Bad.", "Good.")); //initiating dynamic list
    Random rand = new Random(); //for random number
    String randomEmotion = emotions.get(rand.nextInt(emotions.size())); //gets a random emotion,
    System.out.println(randomEmotion + " How are you?");
    String newEmotion = io.nextLine(); //gets an input from the user.