Results 1 to 5 of 5

Thread: How to convert string to int in Java?

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default How to convert string to int in Java?

    I tried Integer.parseInt but it's not working for my name meh.. here is the code


    java Code:
    public int[] intDigits(long Int)
        {
            String s;
            int [] result;
            s = Long.toString(Int);
            for (int i = 0; i < s.length(); i++)
            {
                result[i] = Integer.parseInt(s[(i + 1)]);
            }
            return result;
        }


  2. #2
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    I tried Integer.parseInt but it's not working for my name meh.. here is the code


    java Code:
    public int[] intDigits(long Int)
        {
            String s;
            int [] result;
            s = Long.toString(Int);
            for (int i = 0; i < s.length(); i++)
            {
                result[i] = Integer.parseInt(s[(i + 1)]);
            }
            return result;
        }

    java Code:
    String str = "12345";
    int[] array = new int[str.length()];
    for (int i = 0; i < array.length; i++) {
        array[i] = Integer.parseInt(str.substring(i, i + 1));
    }
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by CynicRus View Post
    java Code:
    String str = "12345";
    int[] array = new int[str.length()];
    for (int i = 0; i < array.length; i++) {
        array[i] = Integer.parseInt(str.substring(i, i + 1));
    }
    Ahh ok so I have to create a new int object array or something

  4. #4
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Not tested. Not the fastest. but this works well in my head.

    java Code:
    public int[] intDigits(long nr)
        {
            int amountOfDigits = Math.floor(Math.log10(nr)) + 1; //yes, math.floor + 1 instead of math.ceil
            int [] result;
            for (int i = amountOfDigits; i > 0; i--)
            {
                result[i] = Math.floor(nr / Math.pow(10, i));
                nr -= result[i] * Math.pow(10, i);
            }
            return result;
        }
    Working on: Tithe Farmer

  5. #5
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    I tried Integer.parseInt but it's not working for my name meh.. here is the code
    java Code:
    public int[] intDigits(long Int)
        {
            String s;
            int [] result;
            s = Long.toString(Int);
            for (int i = 0; i < s.length(); i++)
            {
                result[i] = Integer.parseInt(s[(i + 1)]);
            }
            return result;
        }

    Use String.charAt() to retrieve the character at a specific index. This is the proper way in Java.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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