0

I was wondering how I could split the following data into multiple lists. Here's my input (from a text file, sample recreated here):

aaaa bbbb cccc,ccc,cccc

aaaa-- bbbb

aaaa bbbb cccc-

aaaa bbbb cccc,ccc

aaaa-

aaaa bbbb ccc,cccc,cccc

Seperating each section of text is a blank space. The code I need to make should create three lists, composed of the a, b, and c groups of each entry from the text file with respect to each line, while ignoring any line with the "-". So, my 3 arrays should be filled as follows:

Array1: aaaa, aaaa, aaaa
Array2: bbbb, bbbb, bbbb
Array3: (cccc,ccc,cccc),(cccc,ccc),(ccc,cccc,cccc)

Parenthesis were added to show that the third Array should include all the listed c values a,b, and c all contain strings imported from text files. Here's my code so far:

import java.util.*;
import java.io.*;

public class SEED{

public static void main (String [] args){

    try{

        BufferedReader in = new BufferedReader(new FileReader("Curated.txt"));
        String temp;
        String dash = "-";
        int x = 0;
        List<String> list = new ArrayList<String>();
        List<String> names = new ArrayList<String>();
        List<String> syn = new ArrayList<String>();
        List<String> id = new ArrayList<String>();


        while((temp = in.readLine()) != null){

            if(!(temp.contains(dash))){

                list.add(temp);

                if(temp.contains(" ")){

                    String [] temp2 = temp.split(" ");
                    names.add(temp2[0]);
                    syn.add(temp2[1]);
                    id.add(temp2[2]);

                }else{

                    System.out.println(temp);

                }//Close if

                System.out.println(names.get(x));
                System.out.println(syn.get(x));
                System.out.println(id.get(x));

            x++;


            }//Close if


        }//Close while

    }catch (Exception e){

e.printStackTrace();
        System.exit(99);

    }//Close try

}//Close main

}//Close class

But my output is always: nothing. How could I save these values to 3 separate Arrays or ArrayLists properly?

3
  • well, I'd iterate on all elements and I'd use a lot of if's and string's contains() to do that. Commented Jun 27, 2014 at 0:14
  • Thank you Mr. Santos, I was able to find the following error: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at SEED.main(SEED.java:39) Commented Jun 27, 2014 at 0:14
  • and I would not use a System.exit(99) in your exception handling either Commented Jun 27, 2014 at 0:15

2 Answers 2

1

You're referencing list.get(x), but your x++ and your list.add won't be in synch if you read a line with no dash. So (x) won't be the correct reference.

Why are you doing:

String [] temp2 = list.get(x).split(" ");

Instead of:

String [] temp2 = temp.split(" ");

EDIT

Try:

if(!(temp.contains(dash))){

            list.add(temp);

            if(temp.contains(" ")){

                String [] temp2 = temp.split(" ");
                names.add(temp2[0]);
                syn.add(temp2[1]);
                id.add(temp2[2]);
            }else{

                System.out.println(temp);

            }//Close if

        }//Close if

for(int x = 0; x < names.size(); x++) {

            System.out.println(names.get(x));
            System.out.println(syn.get(x));
            System.out.println(id.get(x));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Oh, that would make it somewhat simpler. Honestly I was just thinking in terms of lists when I wrote the code. But isn't my x++ outside of the if statement that checks for the dash, and therefore shouldn't it update regardless via the while loop?
if you read a line with a dash, you're not adding it to list but you're still incrementing x. So when you reference list.get(x), you'll get an indexoutofbounds exception
That makes sense, but when I tried moving the x++ statement so that it was only incremented when a dash was not detected, the same error occurred. Sorry if these seem like stupid questions, it's been years since I touched CS and I'm a bit rusty
Ahh I just thought about doing that, I used the alternative for loop three different times for each of my arraylists and it ended up working. Thanks so much for your help!
0

Probably an exception is being thrown and your are ignoring it.

Change your catch to

}catch (Exception e){
    e.printStackTrace();
    System.exit(99);

}//Close try

1 Comment

I got an indexoutofbound exception. But why though - aren't my 3 lists getting larger as I add in more values to each? The exception was thrown at Index 1, Size 1, on line 39 of the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.