Results 1 to 8 of 8
  1. #1
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default characters from a string into an integer

    Hi guys,

    me again.. just wondering what's the best way to pull individual character from a string and convert it into an integer in a similar way to the getChars method. I need to do something similar to this but to an integer??

    String text = "To be or not to be";
    char[] textArray = new char[3];
    text.getChars(9, 12, textArray, 0);

    for(char ch: textArray){
    System.out.print(ch);
    }

    output result: not

    I need to do the same to something like this:

    String numbers = "45654878752542442735";
    char[] textArray = new char[1];
    text.getChars(9, 10, textArray, 0);

    int difx;
    difx=Integer.parseInt(textArray[]);
    System.out.println(difx+1); //just to test it

    Obviously this way does not work. Can anyone suggest any help?

    Thanks in advance!

    Lucas
    Last edited by 2potatocakes; 09-17-2008 at 02:35 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

    Default

    This is the best way to do it.

    Java Code:
        public static void main(String[] args) {
            String str = "1234567890";
            for(int i = 0; i < str.length(); i++) {
                int j = Integer.parseInt(Character.toString(str.charAt(i)));
                System.out.println(j);
            }
        }

  3. #3
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    as always eranga.. thanks!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

  5. #5
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    [COLOR=DarkGreen] public static void main(String[] args) {
            String str = "1234567890";
            for(int i = 0; i < str.length(); i++) {
    try{
                int j = Integer.parseInt(Character.toString(str.charAt(i)));
               You might want to check for wrong characters (like the letter A per example) in your array. If you want to do this try the following:
    
    
    [/COLOR]
    Java Code:
    [COLOR=DarkGreen][COLOR=DarkGreen]}
    catch(NumberFormatException e) {
                do whatever you like in here when the 
    }
    }
     [/COLOR][/COLOR]
    The parseInt throws an exception when it's given a wrong parameter. When you catch the exception you can do something usefull (display an error-message to the user or something like that). This will make you code more robust.
    Last edited by Rooneyz; 09-18-2008 at 09:59 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    21

  7. #7
    Rooneyz is offline Member
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    25
    Rep Power
    0

    Default

    Totally agree on that one Eranga, but it's worth considering, that was my intention.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,188
    Blog Entries
    7
    Rep Power
    24

    Default Re: characters from a string into an integer

    Quote Originally Posted by Eranga View Post
    This is the best way to do it.

    Java Code:
        public static void main(String[] args) {
            String str = "1234567890";
            for(int i = 0; i < str.length(); i++) {
                int j = Integer.parseInt(Character.toString(str.charAt(i)));
                System.out.println(j);
            }
        }
    Yuck, better make that:

    Java Code:
                int j =Character.digit(str.charAt(i), 10);
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. String to Integer Conversion in JSP
    By vinikz in forum New To Java
    Replies: 8
    Last Post: 11-10-2010, 03:45 PM
  2. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  3. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 12:14 PM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 08:56 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 11:49 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
  •