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.


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

Results 1 to 2 of 2

Thread: Help With Scanner

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Thanked 0 Times in 0 Posts

    Default Help With Scanner

    I trust I'm posting this in the right forum. You nice people do consider 3 semesters of java still "new," right??

    Anyway, I'm having a problem with Scanner. I have my program prompting me for a file name, and when I type in a file name and hit enter, the program does not seem like it's going to the next step. Now, when I step through in debug mode, it looks like it's making it to a few lines down, where I start scanning the file and counting the number of lines. But my lines variable goes waaaaay past the true number of lines in the file.

    Anyway, what I don't understand, mainly, is why my program seems to get stuck. Is it because it's counting the lines? Also, why does it keep incrementing? My file has 10 lines...

    Here's my code:

      public static void main(String[] args)
        String filename; 
    	 int lines = 0;
        Scanner input_scan = new Scanner(System.in),
        System.out.print("Enter a file name: ");
    	 filename = input_scan.next();
    	 file_scan = new Scanner(filename);
    	 while (file_scan.hasNextLine())

    Here is everything in the text file I'm scanning:

    1234 23 widget1
    3456 123 widget2
    4356 45 widget3
    1056 1234 widget4
    9999 237 widget5
    9322 22 widget6
    5322 755 widget7
    3422 2 widget8
    3333 95 widget9
    1111 15 widget10

    That's it.

    Any help would be greatly appreciated!

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Help With Scanner

    the hasNext method in Scanner doesn't actually move the Scanner to read the next line. You have to manually do it.


Similar Threads

  1. Scanner class error "java.lang.Error"
    By Lheviathan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2009, 02:23 AM
  2. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM
  3. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 08:46 AM
  4. Replies: 1
    Last Post: May 13th, 2008, 08:08 AM