Results 1 to 3 of 3

Thread: Urgent: What's wrong with my code?

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Location
    USA
    Posts
    46

    Exclamation 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

  2. #2

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Location
    USA
    Posts
    46
    Forgot to say. I assign "Price" as double.

    Thanks.

  3. #3
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    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
  •  



Click Here to Expand Forum to Full Width