Results 1 to 4 of 4
  1. #1
    prasads is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Integer parseInt(str) question

    parseInt question:
    I have a string - "00000000030001".
    This is a dollar value from a file. So I need to store last 2 digits as CENTS and rest of the digits as DOLLAR.
    Integer.parseInt works fine for dollar as 000000000300 but it returns 1 for cents instead of 01.

    How can I have the code return 01 for cents value.

    Here is code snippet:
    String str = "00000000030001";
    int startPos = 0;
    int endPos = 12;
    dollar = Integer.parseInt(str.substring(startPos, endPos));
    startPos = 13;
    endPos = 15;
    cent = Integer.parseInt(str.substring(startPos, endPos));
    System.out.println("Dollar: " + dollar + " Cent: " + cent);

    output:
    Dollar: 300 Cent: 1

    I need output as Dollar: 300 Cent: 01

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Check out string formatting.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    System.out.printf("Dollar: %d Cent: %02d ",dollar, cent);

  4. #4
    prasads is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Working with Integer parseInt(String)
    By Rocketz in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 07:31 AM
  2. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 05:36 AM
  3. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 04:25 PM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 03:41 AM

Posting Permissions

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