0

I am trying to write a hierarchal block that puts elements into an ArrayList, all apart of a larger ArrayList. What the block does is take an existing input of text, and adds every line of text into elements of an ArrayList. Each Line is then created as an ArrayList of Strings, with each String being a word on that line (I used the String Split at spaces (" ") to perform this).

My problem is when trying to create this I needed to use the Arrays.asList (because a String Split returns a List)

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new line
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList sentence =  (ArrayList) Arrays.asList(text.get(i).split(" "));
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentence);
            }
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {
                System.out.println(words.get(k).get(i));
            }
        }
    }
};

This was my original Method which return the error. I have since adjusted slightly, which no longer returns an error BUT, doesn't return anything.

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new "line"
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList <String> sentences = new ArrayList();
            String sentence[] =  text.get(i).split(" ");
            sentence = sentences.toArray(sentence);
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentences);
            }
        if (words.size() ==  0)
        {System.out.println("Theres nothing here"); }
        else {
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {

                System.out.println(words.get(k).get(i));}

            }
        }
    }
};

Any feedback or ideas on how to approach a solution is greatly appreciated.

EDIT: Some people asked for the LinetoList function. A majority of the program uses ArrayLists of strings which is why it's so heavily used here.

private static ArrayList<String> LinetoList(JTextArea textArea) {

  String s[] = textArea.getText().split("\\r?\\n");
    ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
    return arrList;
}
  • 1
    1) `String.split()` returns `Array[]` not `List` – Antoniossss Mar 23 '15 at 12:45
  • Feedback: consider renaming your variables. sentences is an ArrayList, and then sentence is an array ... I found that rather confusing. Meaning: try to avoid variable names that are nearly identical. – GhostCat Mar 23 '15 at 12:48
  • Use `ArrayList sentence = new ArrayList<>(Arrays.asList(sentences));` and then `words.addAll(sentence)`. You could also just work with a List instead, as it seems to go out of scope after appending to words anyway. – Soronbe Mar 23 '15 at 12:52
  • Can we see the LineToList function? – ConMan Mar 23 '15 at 13:22

3 Answers3

0

I don't actually follow:

   String sentence[] =  text.get(i).split(" "); // here you have array
    sentence = sentences.toArray(sentence); // WTF?

And now about that WTF mark - you are filling sencence array with content of sentences. WHat is better, I don't see declaration of sentences but I guess it is just empty right? Thats why you see it empty all the time. As for me, lot of strange code and useless comments in it.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • This was a misunderstanding in reading how the toArray function works. I thought it worked similar to the asList in that it simply made one data set into another type (So, list would convert "to an Array"). It was more experimentation with that type so the more thorough (if a little blunt) explanation helps. I found the java documentation description not thoroughly describing how the function works or is implemented. Any thoughts on the other version of this method? I listed two knowing that they both had problems. I feel the other is less of a lack of understanding of its components. – Tordehielbach Mar 23 '15 at 20:38
0

1) Arrays.asList does not return java.util.ArrayList, if you want it to be ArrayList, then you need to do ArrayList sentence = new ArrayList( Arrays.asList(text.get(i).split(" ")));

2) toArray method puts all elements from collection into array. in your case, collection is empty as you are creating almost before you call method

user902383
  • 8,420
  • 8
  • 43
  • 63
  • I did more research, my original reading of the toArray would convert something "to an Array". However knowing this, how feasible would be iterating through each element in the list and using "toArray" to actually make the array. – Tordehielbach Mar 23 '15 at 20:34
0

So after taking a look at the comments on HOW the toArray and asList work, I tried a different approach that was based more on my linetolist function

the result was

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<ArrayList> words = new ArrayList();
        ArrayList<ArrayList> splitLines = new ArrayList();
        ArrayList<String> characters = new ArrayList();
        //^^goes to (As hierarchey )
        ArrayList<String> text = LinetoList(area);
        //text is a String ArrayList of every line in a text
        //LinetoList is a method that returns an ArrayList based on each new line
        for (int i = 0;  i < text.size(); i++) {
            //for each line we have
            String sentence[] =  text.get(i).split(" ");
            ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
            words.add(splitText);
            }
        for (int j = 0; j < words.size(); j++) {
            String sentence [] = text.get(j).split(" ");
            ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
            ArrayList<ArrayList> SplitWords = new ArrayList();
            for (int i =0; i < sentence.length; i++) {
                ArrayList<Character> SplitCharacters = new ArrayList<Character>();
                for (int k = 0; k < splitText.get(i).length(); k ++) {

                SplitCharacters.add(splitText.get(i).charAt(k));

                }
                SplitWords.add(SplitCharacters);
            }
            splitLines.add(SplitWords);

        if (words.size() ==  0)
        {System.out.println("Theres nothing here"); }
        else {
        for (int k = 0; k < words.size(); k++) {
            System.out.println("\n");
            for (int i= 0; i < words.get(k).size(); i ++) {
                System.out.println(words.get(k).get(i));
                }

            }
        System.out.println("That was the original method \n");
        for (int k = 0; k < splitLines.size(); k++) {
            System.out.println(splitLines.get(k).toString() + "\n");

            }
        }
    }       
}
};

I included two different approaches to this problem, one that simplifies things as an ArrayList of words and one that gives an ArrayList of ArrayList of Characters (or a array list of words which hold letters) Thanks for all the help. Hopefully anyone else can take something away from my blunders.