I have the following code but I don’t understand how I can reset the pointer to the starter position:
BufferedReader inp=new BufferedReader(new FileReader(file));
Scanner leggi=new Scanner(inp);
for(int i=0;i<nwords;i++){
while(leggi.hasNext())
if(leggi.next().equals(args[i+2]))
occorrenze[i]=occorrenze[i]+1;
}
inp.close();
I tried
inp.mark(0);
inp.reset();
with no results.
Paul,
I suggest you read through this old thread: Java BufferedReader back to the top of a text file?.
Personally I prefer Jon Skeet’s response, which boils down to “Don’t bother [unless you MUST].”
Cheers. Keith.
EDIT: Also you should ALLWAYS close that input file, even if you hit an Exception. The
finallyblock is perfect for this.EDIT2:
Hope you’re still with us.
Here’s my attempt, and FWIW, you DON’T need to reset the input-file, you just need to transpose your
whileandforloops.And BTW, java.util.Map is perfect for building a frequency table… Parallel arrays are just SOOOOO 90’s. The “default” implementation of Map is the
HashMapclass… By default I mean use HashMap whenever you need a Map, unless you’ve got a good reason to use something else, which won’t be often. HashMap is generally the best allround performer.