What I want to do is, read a file with multiple words in a single line and then add each word in 2d arraylist. This should be such that [[kevin,kevin,kevin][jobs,jobs,jobs]]
the code below works well but it does like this [[kevin,kevin,kevin,jobs,jobs,jobs]]
It should be done by using nested for, but can someone please help?
public void getReference() throws IOException
{
String line=null;
connectRead("computer");
//this is a method that reads a file in a format kevin kevin kevin kevin
try
{
reference.add(new ArrayList<String>());
while ((line=bufferedReader.readLine())!=null)
{
st = new StringTokenizer(line);
for ( int i = 0 ; i < st.countTokens() ; i++)
{
reference.get(i).add(st.nextToken());
reference.get(i).add(st.nextToken());
reference.get(i).add(st.nextToken());
reference.get(i).add(st.nextToken());
}
}
System.out.println(reference);
bufferedReader.close();
}
catch ( IOException e )
{
System.out.println(e);
}
}
text in file looks something like this
kevin usa hacker
wozniak usa hacker
jobs usa hacker
You are always getting references.get(i), where i=0, so whenever new line is read insertion of token starts from the ArrayList at zeroth index.
try this, but this structure looks a bit confusing to me. may be showing structure of your input file helps to make code better.