1

For Java and in reference to this Question how could I save each value in this array to a separate variable?

If string value is:

1234,AAA,30

Variables would be:

var1=1234
var2=AAA
var3=30
Community
  • 1
  • 1
Eng7
  • 552
  • 1
  • 6
  • 23

3 Answers3

2

You can use this in a for loop

String s = "012,345AA,89";
String[] output = s.split(",");
System.out.println(output[0]);
System.out.println(output[1]);
Ugur Tufekci
  • 330
  • 2
  • 13
1

Use:

String str = "1234,AAA,30";
String[] variables = str.split(",");
String first = variables[0];
String second = variables[1];
String third = variables[2];

and that should work

ItamarG3
  • 3,974
  • 6
  • 29
  • 42
1

try this if the array size is not fixed

    String str = "1234,AAA,30";
    String[] arr = str.split(",");
    Map<Object, Object> map = IntStream.range(0, arr.length).boxed()
            .collect(Collectors.toMap(in -> "var" + (in + 1), in -> arr[in], (k, v) -> v, LinkedHashMap::new));
    System.out.println(map);

output

{var1=1234, var2=AAA, var3=30}
Saravana
  • 12,109
  • 2
  • 37
  • 52