-3

The code is trying to take String entries, add them to an array and then stop when no text is written (user just presses enter). Then it is meant to display all of the String items in the array thus far on new lines.

The error in my title is coming up on my if query, and I'm additionally getting error's reading the value 'x' in the for loop as a variable (cannot find symbol).

Can anyone help me out

import java.util.Scanner;
import java.util.ArrayList;

public class FirstPart {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        ArrayList<String> tillEmpty = new ArrayList<String>();
        int i = 0;
        while (true) {
            System.out.print("Type a word: ");
            tillEmpty.add(reader.nextLine());
            if (tillEmpty[i].isEmpty()) {
                break;
            } else {
                i++;
            }
        }
        System.out.println("You typed the following words: ");
        for (x = 0; x < tillEmpty.size; x++){
            System.out.println(tillEmpty.get(x));
        }

    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Anthrackz
  • 15
  • 1
  • 6
  • 2
    [array required, but ArrayList found](http://stackoverflow.com/questions/20067956/array-required-but-arrayliststring-found). [What does a “Cannot find symbol” compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) Googling your error is always a good start. – Bernhard Barker Aug 13 '15 at 11:52

4 Answers4

1

The error message is telling you exactly what is wrong. You've got an ArrayList and are trying to treat it as if it were an array. It isn't, and you can't use array indices, [i] on it. Instead use the get(...) method as any tutorial will tell you (and which I strongly recommend that you read -- Google can help you find one).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you, this worked. I assumed that arraylist was actually just the code of an array (just learning). But have since looked them up and learned a bit about the major differences (arrays fixed length, arrays hold one data type, array CAN store primitives and not just objects... etc.) – Anthrackz Aug 14 '15 at 11:12
0

Your tillEmpty is not an array but an arraylist...you have to use tillEmpty.get(i).isEmpty instead of tillEmpty[i]

Abbel
  • 320
  • 1
  • 9
0

You missed int in your for loop:

for (int x = 0; x < tillEmpty.size; x++){
            System.out.println(tillEmpty.get(x));
        }
sjain
  • 23,126
  • 28
  • 107
  • 185
0

You are accessing list as Array, use below code, this should work:

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        ArrayList<String> tillEmpty = new ArrayList<String>();
        int i = 0;
        while (true) {
            System.out.print("Type a word: ");
            tillEmpty.add(reader.nextLine());
            if (tillEmpty.isEmpty()) {
                break;
            } else {
                i++;
            }
        }
        System.out.println("You typed the following words: ");
        for (int x = 0; x < tillEmpty.size(); x++){
            System.out.println(tillEmpty.get(x));
        }

    }
Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41