0

I need to assign variables in a string.

 String s = "102, 145, 163, 124";

I want to assign a separate variable like below;

num1 = 102;
num2 = 145;
num3 = 163;
num4 = 124;

I need to do it programatically since the values will change and increase so whatever is in string s is assigned a variable. Thanks

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

4 Answers4

2

If the values are going to change, you should look into using an ArrayList which will be dynamic and vary with the length of your data.

For instance, you could easily convert all that data to an ArrayList like this:

String string = "102, 145, 163, 124";
ArrayList<Integer> list = new ArrayList<Integer>();
for(String s : string.split(",")) list.add(Integer.parseInt(s.trim())); 

Each item would then be accessible by calling it's index like so:

int num0 = list.get(0);
int last = list.get(list.size() - 1);

Storing data in lists is a dynamic approach that's scale-able.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • How can I loop through the items so that they can be written like int[] xAxis = new int[]{102, 145,163,124}; Thanks – user3339618 Feb 25 '14 at 17:14
1

Because there are a dynamic number of values, you need to use an array (or ArrayList):

String s = "102, 145, 163, 124";

String[] nums = s.split(", ");

// just printing the array
for (int i = 0; i < nums.length; i++) {
    System.out.println(nums[i]);
}

Output of that:

102
145
163
124
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0
StringTokenizer tokenizer = new StringTokenizer(s, ", ");
for (int i = 0; i < tokenizer.countTokens(); i++)
a person
  • 986
  • 6
  • 13
  • How can I keep the resulting variables in an array? Something like int[] arr = new int[resulting variables in the string] – user3339618 Feb 25 '14 at 02:33
  • Define an array int array = new array[tokenizer.countTokens()]. Then array[i] = tokenizer.nextToken() inside loop. – a person Feb 25 '14 at 03:46
  • so far this is what I have done, String str = "102, 145, 163, 124"; String[] nums = str.split(","); int[] newArr = new int[nums.length]; for(int i = 0;i < newArr.length;i++) { newArr[i] = Integer.parseInt(nums[i]); int results = newArr[i]; } int[] xAxis = new int[]{results}; // but this code is not working. I need to keep the results in the int[] xAxis. – user3339618 Feb 25 '14 at 18:00
0

Try to split then parse your strings

String s = "102, 145, 163, 124";
String[] sArray = s.split(",");
List<Integer> integers = new ArrayList<Integer>();
for(String item:sArray){
  integers.add(Integer.parseInt(s.replace(",","")));
}
GhostDerfel
  • 1,533
  • 1
  • 11
  • 18
  • 1
    You might want to also add a trim() to the end of that replace() or else his Strings will throw an IllegalArgumentException or numberFormatException (I can't remember which, but I'm guessing the number one) – Cruceo Feb 25 '14 at 02:55
  • Can be a trim or change the split to "[0-9]*" but I think the Integer.parseInt(string) can parse numbers with white space in the String (can't test right now)... – GhostDerfel Feb 25 '14 at 03:00