Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Can I have two scanners to the same file? (I want to read through one file 2 times)

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    		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:

    	 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.


  2. #2
    Junior Member
    Join Date
    May 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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.

Similar Threads

  1. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  2. Trying to read a CSV file !
    By Cruz182 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 2nd, 2011, 01:30 PM
  3. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  4. how to read file name inside .gz file....
    By kathir0301 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 6th, 2010, 10:06 AM
  5. how to read a PDF file?
    By epezhman in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 15th, 2010, 06:26 AM