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

Thread: Problem with Scanner

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

    Default Problem with Scanner

    Hello everyone, I'm hoping someone here will be able to help me since I can't seem to find any information on a past issue like this.

    What I am doing is reading a plain-text configuration file line by line for a project I am working on. But at the moment I don't seem to be able to even get Scanner to read the file.

    Configuration file in question is attached (not allowed to link to pastebin sorry).

    Distilled test code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class TestScannerMain {
    	public static void main(String[] args) {
     
    		try (Scanner scan = new Scanner(new File("Pony.txt"))) {
     
    			while (scan.hasNextLine()) {
    				System.out.println(scan.nextLine());
    			}
     
    		} catch (FileNotFoundException e) {
     
    			e.printStackTrace();
    		}
     
    		System.out.println("Done");
    	}
     
    }

    I have tried -
    1. Recreating the file
    2. Using a different file (Using a file I have created by hand seems to work but I need to use the existing files.)
    3. Changing the extension (It originally was an .ini file but it is just plaintext with a different extension)

    At this point I'm thinking it has to do with how the textfile is encoded but I have no idea what the arguments for the Scanner constructor using them for, and I can't seem to find any documentation with this information. Any ideas?

    And thanks in advance for any help!
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,101
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Problem with Scanner

    don't seem to be able to even get Scanner to read the file.
    What happens when the program is compiled and executed?

    The code works OK for me with my test file as input.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Problem with Scanner

    It outputs to my last sysout, I worded that badly, it will actually execute if I used a .txt file that I created. I've also ruled out a permissions issue as well.

    --- Update ---

    Quote Originally Posted by Norm View Post
    The code works OK for me with my test file as input.
    Yes, that's what's weird, if I try to output the config file I have hasNextLine() returns false and the output loop never executes.

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

    Default Re: Problem with Scanner

    Found the problem -

    In the configuration file there is a Unicode character in line 19. Adding the "UTF-8" argument to the Scanner constructor resolved this issue. Now I feel like an idiot.

    Thanks for the help anyways!

Similar Threads

  1. Scanner problem
    By melki0795 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2013, 04:13 AM
  2. [SOLVED] Scanner problem
    By sentimentGX4 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 30th, 2012, 06:09 PM
  3. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  4. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  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