• 21 Dec 2008 /  Programming 2 Comments

    There are just some things that google can never tell you, and no matter how hard you try to search for it you may never find what you are looking for. That happened to me when I tried to find out how to convert a String to an Integer array in Java. So, here’s the simple function that will do this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    public static int[] strToInt (String inp) {
       int[] toRet = new int[inp.length()];
       int i = 0;
       while(i < inp.length()) {
          toRet[i]=(int)inp.charAt(i);
          i++;
       }
       return toRet;
    }

    This function goes through every character of the string and gets the integer value of the character and adds it to the integer array. When it’s done adding everything, it returns the integer array with the same length as the string.

    (PS: Sorry for unindented code; I’ll have to find a better code plugin..)

    Posted by Matt @ 8:04 am

    Tags: ,

2 Responses

WP_Blue_Mist
  • win Says:

    OMG, LERN2FOR!

  • Matt Says:

    Haha, I would learn 2 for, but I don’t feel like it and I like wasting lines of code =D. While loops work for me, I’ve been using them for years, I like wasting lines of code, and I may or may not be mentally challenged in certain areas.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.