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 8 of 8

Thread: Problem with Scanner

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Problem with Scanner

    So I have a text file which is 47 kb big, it includes database information.

    Basically I have a problem the scanner only seems to output up to a random line and then completely stop.

    Scanner x = null;

    try {
     x = new Scanner(new File ("database.txt"));
    }
    catch (FileNotFoundException e){
    System.err.println("File does not exsit");
    }
     
    while(x.hasNextLine) {
    String contentsOfFile = x.nextLine();
    System.out.println(contentsOfFile);
     
    }
     
    x.close();

    It seems to only print out up till line 351 and then stop.

    Anybody know what is going on here?


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with Scanner

    I'd hazard a guess to say that there is some funky encoding in the text file that is messing around with the Scanner. Try specifying the charset to something like UTF-8 and see if that helps:

    x = new Scanner( new File ("database.txt"), "UTF-8" );

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    ProgrammablePeter (January 1st, 2014)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner

    That made it alot better, but still. I added a counter, notepad++ says there is 998 lines and the counter says there is 610 lines, 300 lines of the text file is missing! So frustrating!

  5. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with Scanner

    Drop the file in Pastebin. Whatever is going on is an issue with the text file, not the code.

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner

    database.txt - Pastebin.com

    Thanks for helping man, appreciate it!

  7. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with Scanner

    Well I saved the file you shared and made a few modifications to your code (just sanity stuff, nothing really changed)
    		Scanner scanner;
    		int lineNumber = 0;
     
    		try {
    			scanner = new Scanner(new File("database.txt"));
     
    			while (scanner.hasNextLine()) {
    				String line = scanner.nextLine();
    				System.out.println(lineNumber + ": " + line);
    				lineNumber++;
    			}
     
    			scanner.close();
     
    		} catch (FileNotFoundException e) {
    			System.err.println("File does not exsit");
    		}

    and it picked up all 997 lines.

    Try copying the contents of the file into a new, fresh text file (just use plain old notepad). This is just a weird edge case of character encoding gone wrong.

  8. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Scanner

    Just out of curiosity what operating system are you running? 32-bit/64-bit?

    --- Update ---

    Wow that is super weird, I downloaded the file from pastebin that I uploaded and then slammed it into the package folder and it printed all 997 lines.. that is so weird! Never ever opening that file again! That's man, you're superb!

  9. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Problem with Scanner

    Glad to help.

    Happy coding

Similar Threads

  1. [SOLVED] Problem with Scanner
    By Twily in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 10th, 2013, 05:02 PM
  2. Scanner problem
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2013, 05:13 AM
  3. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  4. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  5. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM