aspose file tools*
The moose likes Java in General and the fly likes iterating over a file using arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "iterating over a file using arraylist" Watch "iterating over a file using arraylist" New topic
Author

iterating over a file using arraylist

karen mansukhani
Greenhorn

Joined: May 04, 2011
Posts: 7
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

Joined: Nov 20, 2010
Posts: 109
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.


For java examples,ebooks,interview questions,visit this blog
http://periodicupdates.blogspot.com/
karen mansukhani
Greenhorn

Joined: May 04, 2011
Posts: 7
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
karen mansukhani
Greenhorn

Joined: May 04, 2011
Posts: 7
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

Joined: Nov 20, 2010
Posts: 109
karen for example check this and for more information about stringtokenizer check this
karen mansukhani
Greenhorn

Joined: May 04, 2011
Posts: 7
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

Joined: Aug 06, 2009
Posts: 33
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

Joined: May 04, 2011
Posts: 7
HI,

thanks for your reply. So then I dont need to use an arraylist?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
sri ramvaithiyanathan wrote: . . . why dont you use stringtokenizer . . .
Because it says in the API not to use StringTokenizer?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iterating over a file using arraylist