0

I have a string that I want to split with a certain delimiter

private int [] mMaxValues;

    public void setMaximum(String maximum) {
       mMaxValues = splitByDelimiter(maximum, ":");
    }

But the splitByDelimiter method return a string array into an int array

public String[] splitByDelimiter(String list,String delimiter) {
    String[] items = list.split("\\" + delimiter);

    for (String s : items) {
        s.trim();
    }

    return items;
}

What is the best way to fix this problem? I'm guessing that iterating the string array and casting them to integers isn't the best solution.

I could also create a new splitByDelimiter that returns an int array but I'm guessing there is a better solution than that..

Is this a situation where you could use generics (I don't have a lot of experience with generics)?

Thx :)

5
  • 4
    You can't cast a String to an int, you need to parse it and there is no way to do this automagically. Commented Sep 7, 2012 at 9:29
  • I guess your returned String[] only contains Strings that can be parsed to an Integer? In other words it contains numbers as Strings?! Commented Sep 7, 2012 at 9:31
  • 2
    Your for loop isn't doing anything. Strings are immutable, so you're trimming the copy of the strings in array. Commented Sep 7, 2012 at 9:33
  • No, it can also contain Strings that can't be converted to Integers. I use to split any string with a given delimiter a return an array. Commented Sep 7, 2012 at 9:34
  • So what should be done with the Strings in the returned String[] that cannot be parsed to an Integer? Do you simply want to ignore them? It's always useful to provide some sample input and the expected output. Input: "bla:123:456:blub:789", Output: {123,456,789} or something like that ... Commented Sep 7, 2012 at 9:35

2 Answers 2

3

You need to convert string array to int array explicitly. Use:

public void setMaximum(String maximum) {
   Strin[] array = splitByDelimiter(maximum, ":");
   int i = 0;
   mMaxValues = new int[array.length];
   for (String value : array) {
       mMaxValues[i] = Integer.parseInt(value);
       i++;
   }
}

Also you need to handle few cases which may cause NullPointerException :

  1. maximum is null
  2. array is null
  3. NumberFormatException may be raised while parsing Integer.parseInt(value), handle it.
Sign up to request clarification or add additional context in comments.

Comments

1

Loop over the string array and store them in an int array.

String input = ...;
String[] parts = input.split(':');
int[] result = new int[parts.length];
for (int n = 0; n < parts.length; ++n)
{
  result[n] = Integer.parseInt(parts[n]);
}
return result

4 Comments

It appears he want to trim() each of the parts before parsing them.
That's a trivial change: parts[n].trim()
@iccthedral No it won't. Try Integer.parseInt(" 8 "). Gives a NumberFormatException.
@iccthedral No, it does not. It throws NumberFormatException in such cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.