pirisu
pirisu

Reputation: 43

Converting a String with spaces and numbers to an int array with just numbers

I want to convert String lotto int an integer array. String lotto is composed of a certain amount of numbers between 1 and 99 and is only composed of 1 and 2 digit numbers. (Example: String lotto might look like "1 34 5 23 7 89 32 4 10 3 6 5".)

I am trying to solve the problem by converting the String to char[] and then the char[] to an int[]. My logical behind converting it to the char[] is make it possible to format the numbers for the int[].

Here is what I have so far:

public static int[] conversion(String lotto)
{
    char[] c = lotto.toCharArray();
    int[] a = new int[c.length];
    for(int i = 0, j = 0; i < c.length; i++)
    {
        if(c[i] != ' ' && c[i+1] != ' ')
        {
            a[j] = c[i] + c[i+1];
            i+=2;
            j++;
        }
        else if(c[i] != ' ' && c[i+1] == ' ')
        {
            a[j] = c[i];
            i++;
            j++;
        }
    }
    return a;
}//end of conversion method

I am still working on the rest of the program but I know that c[i] + c[i+1] with return an ASCII value or a different int rather than combining the two chars together (Example of what I want: '3' + '4' = 34.)

How do I fix this problem?

Upvotes: 4

Views: 4071

Answers (6)

zapl
zapl

Reputation: 63955

I'd do it like so:

    public static int[] string2array(String s) {
        return util1(s, 0, 0, false);
    }

    private static int[] util1(String s, int n, int l, boolean b) {
        if (s.isEmpty()) {
            return b ? util2(l, n, new int[l + 1]) : new int[l];
        }
        if (Character.isWhitespace(s.charAt(0))) {
            return b ? util2(l, n, util1(s.substring(1), 0, l + 1, false)) : util1(s.substring(1), 0, l, false);
        }
        return util1(s.substring(1), n * 10 + Character.digit(s.charAt(0), 10), l, true);
    }

    private static int[] util2(int idx, int value, int[] array) {
        array[idx] = value;
        return array;
    }

Result of Arrays.toString(string2array("1 34 5 23 7 89 32 4 10 3 6 5")) is

[1, 34, 5, 23, 7, 89, 32, 4, 10, 3, 6, 5]

http://ideone.com/NCpOQc

Upvotes: 1

Steve Siebert
Steve Siebert

Reputation: 1894

Fun with Java 1.8...one line:

int[] nums = Pattern.compile("\\s")
   .splitAsStream("1 34 5 23 7 89 32 4 10 3 6 5")
   .mapToInt(Integer::valueOf)
   .toArray();

want to have the int array sorted? Still one line:

int[] nums = Pattern.compile("\\s")
   .splitAsStream("1 34 5 23 7 89 32 4 10 3 6 5")
   .mapToInt(Integer::valueOf)
   .sorted()
   .toArray();

Upvotes: 1

jhillkwaj
jhillkwaj

Reputation: 46

If you don't care about converting to a character array then you could just use the .split() method

    String[] nums = lotto.split(" ");
    int[] a = new int[nums.length];
    for(int i = 0; i < a.length; i++)
    {
        a[i] = Integer.parseInt(nums[i]);
    }
    return a;

Upvotes: 3

Shadow
Shadow

Reputation: 4006

Using strings split function, you can split the string up by spaces like so:

string[] lottoArray = lotto.split(" ");

Then, you can loop through the array and put the values into a int array:

int[] numbersArray = new int[lottoArray.length];
for (int i = 0; i < lottoArray.length; i++)
    numbersArray[i] = Integer.parseInt(lottoArray[i]);

Upvotes: 1

Vince
Vince

Reputation: 15146

Split the String containing the numbers

String str = "1 2 33 64";
String[] numbers = str.split(" ");

Create an int[] the same size as the String[]:

int[] array = new int[numbers.length];

Loop through numbers, parsing each value and storing it in the int[]:

for(int i = 0; i < numbers.length; i++)
    array[i] = Integer.parseInt(numbers[i]);

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44824

Try using String.split

String numbers[]  = lotto.split (" ");

Upvotes: 1

Related Questions