Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parse table columns into array

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".

like image 928
user3452464 Avatar asked Apr 16 '26 02:04

user3452464


1 Answers

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);
like image 187
Siavosh Avatar answered Apr 18 '26 14:04

Siavosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!