0

I have a text file that contains lines, with words separated by commas. I am trying to create an array inside of another array so i can call specific words from the text file. Right now I can save each line of the text file into an array, but I don't know how to call a specific word in a line.

TEXT FILE

Hat, dog, cat, mouse
Cow, animal, small, big, heavy
Right, left, up, down ,behind
Bike, soccer, football, tennis, table-tennis

CODE

animals = new Scanner(new File("appendixA.txt"));
// code for number of lines start
File file =new File("appendixA.txt");

if(file.exists()) {

    FileReader fr = new FileReader(file);
    LineNumberReader lnr = new LineNumberReader(fr);

    int linenumber = 0;

    while (lnr.readLine() != null) {
        linenumber++;
    }

    lnr.close();

    // code for number of lines end

    String animal[] = new String[linenumber];

    for (int i = 0; i < linenumber; i++) {
        String line = animals.nextLine();
        animal[i] = line;

        for (int j = 0; j < animal[i].split(",").length; j++) {
            String animalzzz[] = animal[i].split(",");


        }


    }
}
Jacob
  • 168
  • 2
  • 13
htmlnoob
  • 25
  • 1
  • 5
  • 1
    Do you want to keep the commas or do you just want to words in the array? – GBlodgett May 03 '18 at 14:12
  • 3
    Where do you use multidimensinal arrays? What is/is not working? Did you [debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? --- Some remarks on oyur code: Please fix your indentation and code style --- `String animal[]` - You should write the array-brackets after the type, not after the variable name, since they influence the type: `String[] animal` --- The amount of blank lines is quite high, please use them sparsely. Right now, the blank lines do not improve readability. – Turing85 May 03 '18 at 14:12
  • @GBlodgett I don't want the commas, I understand i can use the .split() function but I dont know where to go from there – htmlnoob May 03 '18 at 14:15
  • `LineNumberReader` is useless when you want to count all lines in the file. it is only useful when you are interested in line number of rows while reading the file – Sharon Ben Asher May 03 '18 at 14:17
  • Possible duplicate of [How to make an array of arrays in Java](https://stackoverflow.com/questions/4781100/how-to-make-an-array-of-arrays-in-java) – xxxvodnikxxx May 03 '18 at 14:20
  • If you want to avoid knowing the size of the array, use ArrayList – vincrichaud May 03 '18 at 14:24

1 Answers1

1

You need to use the 2-dimensional array. 2D arrays are used to represent a table structure. In your case, you have multiple rows and each row have multiple values. So, one array would be used to represent a row and another array will be used to represent the column values in each row.

        String[][] animal = new String [linenumber][];

        for (int i = 0; i < linenumber; i++) {
                String line = animals.nextLine();
                String[] oneRowAnimals = line.split(",").trim();
                for(int j=0; j<oneRowAnimals.length; j++) {

                    // Here you are storing animals
                    animal[i][j] = oneRowAnimals[j];
                }
        }
        // Now you can access them by index.
        // For exmaple, this would give you "dog"
        String animalName = animal[0][1]; 
Yogesh Badke
  • 4,249
  • 2
  • 15
  • 23