Can I have two scanners to the same file? (I want to read through one file 2 times)
So what I need to do in this program is go through a file and check for symbols/labels, then im storing those labels in an array for later access. then go through again and convert the instructions.
now after i go through the file once, i want to go through it again, starting from the top, but im not sure how this is done.
i tried this:
Code :
Scanner sc = new Scanner(new File("asm.asm"));
Scanner sc2 = new Scanner(new File("asm.asm"));
Assuming that each one would point to the beginning of the file, so i could go through using sc then when im done go through and use sc2 which would still be at the beginning of the file since its a different scanner.
but i get an error when it gets to the 2nd scanner:
Code :
while(sc2.hasNextLine())
{
//error happens on this line, no such element exception
input = sc.nextLine();
}
So i'm wondering am I going about this the wrong way or am I just not executing my idea properly? If there is an easier way to do this please let me know (like maybe there is a command to reset the scanner to the beginning of the file that i dont know about). Thank you for the help.
Re: Can I have two scanners to the same file? (I want to read through one file 2 time
wow just found it, i used sc when i was reading in the input. always seems like i figure it out once i ask for help.
Re: Can I have two scanners to the same file? (I want to read through one file 2 time
Sometimes all you need to do is lay out your logic ;) Mark the thread as solved please.