0

Eg if I have a string "{1,2,3,4,5}" I would like to get an int[] object from that string.

I have looked a bit at Janino and Beanshell, but can't seem to find the correct way to get them to do this for me.

I am looking for a generic solution, one that works for all types - not only integer arrays.

Seki
  • 11,135
  • 7
  • 46
  • 70
Mellson
  • 2,918
  • 1
  • 21
  • 23
  • What is the format of the string? JSON? `toString`? – Bart van Heukelom Jun 18 '12 at 10:35
  • Just a normal Java string containing something like "{1,2,3}", "1", "Stackoverflow" and so forth. – Mellson Jun 18 '12 at 10:37
  • 1
    That's a *different* question, since now you're asking about converting an essentially arbitrary string into, apparently, arbitrary Java objects. What are you really trying to do? – Dave Newton Jun 18 '12 at 10:38
  • I am trying to create objects based on what a user types in a field. So if a user types {1,2,3}, I want an int array. If the user types Hello, I want a string. – Mellson Jun 18 '12 at 10:44
  • 1
    ok, do not go this way... it's a lot of parsing... there is no clean way to do so... you never know, what the user is typing into your textfiel – headgrowe Jun 18 '12 at 10:45
  • You are proposing that the user will type in data in some format or language. You need to define this format or language precisely before you ask us how to parse it. This question is much too unclear right now. Are commas or curly braces significant? How exactly? Why? What about other characters? – Christoffer Hammarström Jun 18 '12 at 10:50
  • So "{1, Hello, 3, 4, 5}" should yield what? String[]? How about "{1.1, 2, 3e-4, 0x4, 0101}"? float[]? double[]? Need more constraints. You are not asking for a "generic" solution, you are looking for a "universal" solution. – pap Jun 18 '12 at 11:00
  • Yes, I know that this is in no way the correct way to do it. I am rethinking the question and problem - will update accordingly. Thanks for all your comments! – Mellson Jun 18 '12 at 11:03

3 Answers3

2

looks like a parse problem to me. look at the string methods :)

the code could look like:

String s = "{1,2,3,4,5}"
String justIntegers = s.substring(1, s.length()-1);
LinkedList<Integer> l = new LinkedList();
for (String string: justIntegers.split(','))
l.add(Integer.valuesOf(string));
l.toArray();

if you are using strings to send/save objects pls use xml or json...

headgrowe
  • 529
  • 1
  • 5
  • 17
  • Thanks for the effort, that would work if all I am getting is integer arrays. However I would like a generic solution. – Mellson Jun 18 '12 at 10:40
  • 2
    for a generic solution, use "XStream" it's a java-lib to convert java objects to xml-strings and back... just 1 line code each... really usefull – headgrowe Jun 18 '12 at 10:42
  • Thanks, I will try that straight away – Mellson Jun 18 '12 at 10:45
  • you can just read/convert xml-Strings... not normal strings... its a way to send/save java-objects via String or byte[] – headgrowe Jun 18 '12 at 10:49
2

Take a look at https://stackoverflow.com/a/2605050/1458047

The answer proposes a few options.

Community
  • 1
  • 1
2

Better to use Regular Expression.Not necessary that your String is Array it could be any String which contains numbers.

        String s="{1,2,3,4,5}";
        Pattern p = Pattern.compile("-?\\d+");
        Matcher m = p.matcher(s);
        List<Integer> list=new ArrayList<Integer>();
        while (m.find()) {
          Integer num=new Integer(m.group());

          list.add(num);
        }

        System.out.println(list);

Output:

[1, 2, 3, 4, 5]
amicngh
  • 7,831
  • 3
  • 35
  • 54