Results 1 to 6 of 6
  1. #1
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Reading a file with Scanner

    Now before you read my code:
    I tried reading a word (name=f.next()) from a file called names.txt which contained the name "George". Reading that was successful. But i am now trying to read a file named hybeldata.txt which contains following:
    10;2012;0;0; 6000; 7000;

    And i believe the problem is the semicolon. But i have a textbook and other resources - and none of them have been much of help. Because they have focused on using a university made package for students. So i do not know how to tell the program to "read the next int after ;".


    Java Code:
    import java.util.*;
    import java.io.*;
    
    class Name{
        public static void main(String[]args){    
    
    	Scanner f;
    	try{
    	    f=new Scanner(new File("hybeldata.txt"));
    	    
    	    int month=f.nextInt();
    	    int year=f.nextInt();
    		int totalProfit=f.nextInt();
    		int monthsDrift=f.nextInt();
    		int rentLower=f.nextInt();
    		int rentTop=f.nextInt();
    
    		System.out.println("Test:"+totalProfit);
    
    		
    
    	}catch(Exception e){
    	    System.out.println("Reading file failed.");
    	}
    	
    
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Reading a file with Scanner

    You have to use a delimiter. Recommended reading: Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Reading a file with Scanner

    I have little knowledge of delimiters. But i assume i am guessing right when i say it's a method that helps you separate x from x in a file. Example a word from another word etc. And that the default delimiter may be " ".

    Anyhow i did some research to get enlightened about the subject (hard as it is that i have poor vocabulary), and found no good results.

    Here is my code TRYING to use a delimiter:
    Java Code:
    import java.util.*;
    import java.io.*;
    
    class Name{
        public static void main(String[]args){    
    
        Scanner f;
    try{
        f=new Scanner(new File("hybeldata.txt"));
        f.useDelimiter(";");
        while(f.hasNextLine()){
        int month=f.nextInt();
        int year=f.nextInt();
        int totalProfit=f.nextInt();
        int monthsDrift=f.nextInt();
        int rentLower=f.nextInt();
        int rentTop=f.nextInt();
        }
    
    
    		
    
    	}catch(Exception e){
    	    System.out.println("Reading file failed.");
    	}
    	
    
        }
    }

    And as reminder hybeldata.txt contains following:
    10;2012;0;0; 6000; 7000;

    Still does not work, it goes to catch exception block.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Reading a file with Scanner

    What is the Exception?

    The delimiter isn't always a single ';'. It's a single ';' followed by 0 or 1 spaces, right? Check out the Pattern class for ways to handle that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Reading a file with Scanner

    Quote Originally Posted by KevinWorkman View Post
    What is the Exception?

    The delimiter isn't always a single ';'. It's a single ';' followed by 0 or 1 spaces, right? Check out the Pattern class for ways to handle that.
    Yes, the delimiter is semicolon and 2 spaces but i added the spaces into it (and still failed).

    The exception i get is what is in the exception code. So when i run program i get "Reading file failed".

    EDIT:
    I entered e.printStackTrace(); (Haven't touched the subject in school, but i recalled from a other topic i made to have that into the catch block).

    So here is the error:

    Reading a file with Scanner-exception.png
    Last edited by Games2Design; 10-03-2012 at 09:55 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Reading a file with Scanner

    I hate using Scanner for this sort of thing as it tries to do it all for you, so you can't break it down easily.

    Read a line then use the split() method on String to break it into it's constituent parts.
    The split method uses a regex, like the Scanner does for its delimiters, so you'll be using the same concepts.
    The advantage here is that this will give you a String[] that you can then print out the elements of and see exactly how the split worked.
    Then, once you've managed to get that right, you can worry about Integer.parseInt.

    Baby steps.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 10:44 AM
  2. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 08:57 AM
  3. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 04:26 PM
  4. Java scanner reading txt documents
    By csisdifficult in forum New To Java
    Replies: 3
    Last Post: 04-21-2011, 09:24 PM
  5. Scanner reading accented characters
    By Phenomena in forum New To Java
    Replies: 2
    Last Post: 04-29-2010, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •