3

I am iterating over the table and I want to parse column 1 and column 2 into 2 separate arrays. I am not sure how to do this. Here is the code I have written so far:

while (line !="table_end"){
     for (line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()){
                         String[] rowData = line.split("\t");

                         for (int i = 0; i < rowData.length; i++) {
                             String id = rowData[0];// ids
                             System.out.println(id);
                             String nid = rowData[1];// names
                             System.out.println(nid);
                             //create arrays out of this

The loop extracts the first (rowData[0]) and second row data (rowData[1]) from the table, and i want to store all rowData[0] entries in one array, and rowData[1] entries in another array.

Besides that, i get an error when the loop ends, because it includes the line "table_end".

The table looks like this:

data0  data1 number number number etc
data0  data1 number number number etc
data0  data1 number number number etc
 this goes on for about 1000 rows
table_end

I am a beginner in Java and I need advise on how to store each rowData[0] and [1] in separate arrays and how to not read the last line, "table_end".

2 Answers 2

1

well it can be managed in the while with a condition in this case

while (line !="table_end"){      
       for (line = bufferedReader.readLine(); line != null; 
                    line = bufferedReader.readLine()) { 
           if ( !line.equals("table_end"){
               //Do other things here
           }
    }
  }

I assumed you just don't need "table_end" to be included hop it helps and for storing the rowData[0] in 1 array and rowData[0] in another you have 2 options either if the length is specific have 2 loops inside the for and then add these elements to 2 other arrays or if the length is not specific adds them to ArrayList and later on convert ArrayList to array of String

 ArrayList<String> firstArray = new ArrayList<String>(); 
 ArrayList<Strign> secondArray = new ArrayList<String>();

and inside the loop you'll have

       firstArray.add(rowdata[0);
       secondArray.add(rowdata[1);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, figured out how to do this with ArrayList, but still confused about how to make it not read the last line.
maybe I'm confused about what you need your code does but according to my understanding if you change this line for (line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) with for (line = bufferedReader.readLine(); line != null && !(line.equals("table_end")); line = bufferedReader.readLine()) it should do what you need let me know if it helps
0

As you probably do not know how many bufferedReader will yield you should put your ids and you nids into ArrayList<String> objects (e.g. ids and nids) and convert those to arrays in the end.

Comments

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.