Results 1 to 4 of 4
  1. #1
    eramas is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default String to int (without the use of Integer or Character classes)

    Hi,

    I have an assignment which requires me to manipulate a string that only contains number and to transform it into an "int".
    Unfortunately I can't use the very cool Integer wrapper class nor the Character wrapper class.

    Can anyone point me in the right direction (I assume I'll need to take the string into a char array at some point)?

    Thanks a bunch.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: String to int (without the use of Integer or Character classes)

    Hint:
    Java Code:
    System.out.println((int) '1');

  3. #3
    eramas is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: String to int (without the use of Integer or Character classes)

    Haha thanks, I know how to do it with a single char.
    Java Code:
    private int convert(char c)
    {
       int i = c - '0';
       return i;
    }
    But I don't see how to deal with a string ("97815")

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: String to int (without the use of Integer or Character classes)

    You said it in your first post. Did you think to look at the String class in the Java API to see if it has a method to help?

Similar Threads

  1. Replies: 1
    Last Post: 11-01-2011, 05:35 PM
  2. Integer value in Character Array
    By AwesomeGuy in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 07:41 PM
  3. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 04:04 AM
  4. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  5. REGEX - predefined character classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:42 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
  •