Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

iterating over a file using arraylist

 
karen mansukhani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to read in a file and store it into an arraylist. My method does that. My arraylist has data of the form 125, 20, 13,1, 0,0,0 ....ad. 23,45,9,0,0,0...ad and so on. I need to divide the arraylist into arrays such that for the first iteration my array x contains data till the first ad, for the next iteration it contains data from 23 to d next ad n so on. How can I do this. This is my function:


It works only for the first line uptil the first ad and then stops. How can I make it iterative. Please help me.


Thanks,
Karen
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi karen welcome to coderanch...
why dont you use stringtokenizer...
As far as my understand you have to store values in array from some value(say 1,5...) upto ad.
By using StringTokenizer, you can easily achive it.
 
karen mansukhani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you so much for your reply. Can you please tell me how to do it using tokenizer. I tried earlier but could not manage to do it.
It would be great if you could give me that part of code for tokenizer.


Thanks,
Karen
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Your code contains an error in line 43. Don't compare strings using == in Java, because it doesn't do what you think it does. You must use the equals() method instead:
 
karen mansukhani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I will change that. But that part is working fine. However its the while loop with the iterator that issues. It iterates only till the first ad and then stops. I need to get the value of x do some processing and then get a new value of x do some more processing and so on. I only manage to get the first value of x. If I add the for loop it goes on infinitely.
 
sri ramvaithiyanathan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karen for example check this and for more information about stringtokenizer check this
 
karen mansukhani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I have used the string tokenizer to read the file as its a csv file. My file is read and now stored into an arraylist. If i again use tokeniser I would face the same problem of how 2 continue where I left off. As in my file there will be around 3000 different values of x. The value of x changes 3000 times. This is how I am reading the file.

public void read(String fileName)
{
try
{
File featureVector = new File(fileName);

BufferedReader bufRdr = new BufferedReader( new FileReader(featureVector));
String line = null;

while ((line = bufRdr.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreElements()) {
tokens.add(st.nextToken());
}
}
}
catch(Exception e)
{
System.out.print(e);
}
}
 
Amit ChaudhariC
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming your have created a arraylist with the elements {a,b,end,c,d,end..end}
i would create a stringbuilder sb will all the elements.
StringTokenizer st = new StringTokenizer(sb.toString(),"end");
while st.hasmoretokens{
string s = st.nextToken;
do your operation.
}

There might be better ways of doing this though.

Regards,
Amit
 
karen mansukhani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

thanks for your reply. So then I dont need to use an arraylist?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sri ramvaithiyanathan wrote: . . . why dont you use stringtokenizer . . .
Because it says in the API not to use StringTokenizer?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic