|
-
Oct 21st, 2002, 08:41 AM
#1
Thread Starter
Banned
Urgent: What's wrong with my code?
Hi.
I'm using a StringTokenizer to read column data, which is seperated by tabs, from a .txt file. And one of the column fields is $18.00. The followings is part of my code.
=====================================
NumberFormat NF = NumberFormat.getInstance();
myStrings = new StringTokenizer(strLine, "\t");
if (myStrings.countTokens()==8) {
ItemNo = new Integer(myStrings.nextToken()).intValue();
System.out.println (ItemNo);
Description = myStrings.nextToken();
System.out.println(Description);
Price = NF.parse(myStrings.nextToken());
System.out(Price);
}
=====================================
The program can show ItemNo and Description on the putput screen when I complie it, but it can't show "Price". Instead, an error message: "Exception in thread "main" java.lang.NumberFormatException: $18.00" comes out.
Would you tell me how to fix it?
Thanks a lot!!!
Jacob
-
Oct 21st, 2002, 09:06 AM
#2
Thread Starter
Banned
Forgot to say. I assign "Price" as double.
Thanks.
-
Oct 21st, 2002, 09:42 AM
#3
Addicted Member
I have not tried this, as my Java environment is screwed. I am assuming that java is saying that the string for "Price" is not purely numbers. Therefore when you try to parse it, you get an error.
The NumberFormatEXception is thrown because the token "$18.00" is not just a number, it contains characters aswell which it doesn't like.
If you know that the beginning of the price string will be a '$' you could remove it using substring, then parse the resulting string...?
HD.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|