Reputation: 1
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?
Upvotes: 0
Views: 262
Reputation: 1908
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));
}
Upvotes: 1
Reputation: 8338
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
Upvotes: 0