Results 1 to 8 of 8
  1. #1
    getName() is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default Using scanner for CSV

    Hello,

    I am trying to use scanner to read data from a CSV data file like this:

    Java Code:
    1,000;     2,000;    3,000;    4,000;    5,000;
    6,000;     7,000;    8,000;    9,000;   10,000;
    and so on (there are several whitespace in between the columns). Every column represents measurement data.
    It is supposed to be working using code like this:

    Java Code:
    public final void processLineByLine() throws FileNotFoundException {
    
    Scanner scanner = new Scanner(new BufferedReader(new FileReader(fFile)));
    
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
                 processLine( scanner.nextLine() );
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
      }
    
      protected void processLine(String aLine){
        //use a second Scanner to parse the content of each line
    
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("; *");
    
        if ( scanner.hasNext() ){
            if (scanner.hasNextDouble()){
                log("Double " + scanner.nextDouble());
                //log uses simply system.out.print
            }
            if (scanner.hasNext()){
                log("String " + scanner.next());
            }
        }
        else {
        }
        scanner.close();
    }
    I've found in another forum that "; *" (; and an undefined amount of whitespace) should work with my example. But I am getting as a result

    Double 1.0
    String 2,000
    Double 6.0
    String 7,000

    So only the first two columns are parsed. And only the first one is recognised as Double. I'd like to have them all as double. Can anyone help me?

    Thanks in advance.

    Moderator Edit: Code tags added
    Last edited by Fubarable; 06-20-2010 at 01:50 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    As for your problem, have a careful look at the line indicated below:

    Java Code:
      protected void processLine(String aLine){
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("; *");
    
        if ( scanner.hasNext() ){ // **** here ***


    You're not going to get much loop-action with an if block. :)

    Best of luck, and again, welcome!

  3. #3
    getName() is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi Fubarable,

    thanks for the editing. Won't happen again, sorry!
    As for your tipp: yes, that's right, how simple (it was late at night already)! But the problem stays the same changing 'if' to 'while'. Still getting a result as follows:

    Java Code:
    Double 1.0
    String : '2,000'
    Double 3.0
    String : '4,000'
    Double 5.0
    Double 6.0
    String : '7,000'
    Double 8.0
    String : '9,000'
    Double 10.0
    Every second entry is recognised as Double.
    As a delimiter I've used ...(\\s*;\\s*);

    Where is my mistake?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    You may need to add an else to work with your if blocks, but let's see your latest code, in particular your latest processLine(String aLine) method code.

  5. #5
    getName() is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Here it is:

    Java Code:
      protected void processLine(String aLine, int lineNumber){
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("\\s*;\\s*");
        while ( scanner.hasNext() ){
            if ( scanner.hasNextDouble() ){
                log("Double " + scanner.nextDouble());
            }
            if (scanner.hasNext()){
                String col1 = scanner.next();
                log("String : " + quote(col1.trim()));
            }
        }
        scanner.close();
      }
    Point is, I can't even convert the string types into Double afterwards using Double.ParseDouble(sString) although there is apparently no difference in between these entries.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    This code is mucking you up royally:
    Java Code:
            if (scanner.hasNext()){
                String col1 = scanner.next();
                log("String : " + quote(col1.trim()));
            }
    Likely the scanner object does have a next token, and it's a double that you're swallowing and wasting. If you feel you absolutely must have this block, then at least place an "else" before it so that it doesn't get called if a decent double is found. For my part, I'd get rid of it.

  7. #7
    getName() is offline Member
    Join Date
    Jun 2010
    Posts
    11
    Rep Power
    0

    Default

    Man, what a simple 'if' can do to you! Now I get it.
    Need vacations...

    Thanks a lot.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

Similar Threads

  1. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-13-2009, 12:24 AM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  3. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 04:37 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 PM

Tags for this Thread

Posting Permissions

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