2

I have a string s="1,2,3,4,5" . I am using the split() method to split the string then i want to store it into an array of ints.
I tried doing the following but it doesn't work.

int i[]=Integer.parseInt(s.split(","));

I want to know if it is possible to do this without using a loop.

There is something like Array.convertAll in C# but I don't know of a similar thing in java.

Cœur
  • 37,241
  • 25
  • 195
  • 267
yahh
  • 1,175
  • 3
  • 14
  • 41
  • 5
    what's wrong with using a loop? You understand that Array.convertAll is implemented with a loop, right? – Petar Ivanov Dec 15 '11 at 09:01
  • @Petar Ivanov:- i know that but i am writing this code for a coding competition and in it using loops just kills the score. plus i really wonder how come there is no simple single line soln – yahh Dec 15 '11 at 09:03
  • 1
    @yahh its not something you need to do very often. Whatever you are doing with the `int[]` will need a loop as well in which case you may find you don't need the `int[]` – Peter Lawrey Dec 15 '11 at 09:05

5 Answers5

1

In java there is no such way (method) by which you can directly get a int array by passing String array
So u must write some method to do this...
and if there is condition that you must not have to use loop then ...i write a code by adding some more code in Adel Boutros may u get help from this but its better to you loop.. :)

public class Test {
public static void main(String[] args) {
    String s = "1,2,3,4,5";
    int size = 0;
    int[] arr = new int[s.split(",").length];
    findInt(s, arr, size);
    for (int i = 0; i < arr.length; i++) {

        System.out.println(arr[i]);
    }
}

static void findInt(String s, int[] arr, int index) {
    String aa = null;
    if (s == null) {
        return;
    }
    if (s.length() != 1) {
        String text = s.substring(0, s.indexOf(","));
        arr[index] = Integer.parseInt(text);
    } else {
        arr[index] = Integer.parseInt(s);
    }
    if (s.length() != 1) {
        aa = s.substring(s.indexOf(",") + 1, s.length());
    }
    findInt(aa, arr, ++index);
}

}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0

If you don't want to use a loop, you can call a method which does the loop for you.

e.g. (You will need to write the convertToInts method)

int[] ints = convertToInts(s);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

In pure Java, there is no way of doing this.

In Apache Commons (especially Commons Lang), there are transform utilities that only take a function with array and do what you want, you don't have to code the loop by yourself.

belgther
  • 2,544
  • 17
  • 15
0
public static void main (String []args) {
    String s="1,2,3,4,5";
    int size = 0;
    int [] arr = new int [s.length];
    findInt(s, arr, size);
}

void findInt(String s, int [] arr, int index) {
    if (s.isEmpty()) {
        return;
    }
    String text = s.substring(0,s.indexOf(","));
    arr[index] = Integer.parseInt(text);
    findInt(text.substring(1), arr, ++index);
}

PS: My method uses the recursive approach. The variable size will be the actual size of the int array.

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
0

This one will probably fail your contest, but it indeed avoids the loop :)

public static void main(String[] args) throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
    String s = "1,2,3,4,5";
    String s1 = s.replaceAll("(\\d+),?", "arr[i++]=$1;");
    int[] arr = new int[s.split(",").length];
    jsEngine.put("arr", arr);
    jsEngine.eval("var i=0;" + s1);
}

The result is an int[] filled with numbers from your String.

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95