Results 1 to 8 of 8
Thread: Using scanner for CSV
- 06-20-2010, 12:35 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Using scanner for CSV
Hello,
I am trying to use scanner to read data from a CSV data file like this:
and so on (there are several whitespace in between the columns). Every column represents measurement data.Java Code:1,000; 2,000; 3,000; 4,000; 5,000; 6,000; 7,000; 8,000; 9,000; 10,000;
It is supposed to be working using code like this:
I've found in another forum that "; *" (; and an undefined amount of whitespace) should work with my example. But I am getting as a resultJava 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(); }
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 addedLast edited by Fubarable; 06-20-2010 at 12:50 AM. Reason: Moderator Edit: Code tags added
-
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!
- 06-20-2010, 03:43 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
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:
Every second entry is recognised as Double.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
As a delimiter I've used ...(\\s*;\\s*);
Where is my mistake?
- 06-20-2010, 04:03 PM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-20-2010, 04:18 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Here it is:
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.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(); }
- 06-20-2010, 04:20 PM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
This code is mucking you up royally:
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.Java Code:if (scanner.hasNext()){ String col1 = scanner.next(); log("String : " + quote(col1.trim())); }
- 06-20-2010, 04:30 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 11
- Rep Power
- 0
Man, what a simple 'if' can do to you! Now I get it.
Need vacations...
Thanks a lot.
- 06-20-2010, 04:33 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Similar Threads
-
Help With Scanner
By jtmoney0511 in forum New To JavaReplies: 10Last Post: 10-12-2009, 11:24 PM -
Need help with scanner.
By mainy in forum New To JavaReplies: 3Last Post: 07-28-2009, 02:11 PM -
Scanner
By choko in forum New To JavaReplies: 10Last Post: 01-24-2009, 03:37 PM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 01:41 PM -
help with IP scanner
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks