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

4
  • this is java right?: stackoverflow.com/questions/16773599/… Commented Feb 25, 2014 at 2:30
  • yes,sorry forgot to mention that Commented Feb 25, 2014 at 2:30
  • If there will ever be more than 4 numbers, you should look into arrays Commented Feb 25, 2014 at 2:32
  • Are the nums ints? Or Strings? Commented Feb 25, 2014 at 2:35

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I loop through the items so that they can be written like int[] xAxis = new int[]{102, 145,163,124}; Thanks
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

Comments

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(",","")));
}

2 Comments

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)
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)...
0
StringTokenizer tokenizer = new StringTokenizer(s, ", ");
for (int i = 0; i < tokenizer.countTokens(); i++)

3 Comments

How can I keep the resulting variables in an array? Something like int[] arr = new int[resulting variables in the string]
Define an array int array = new array[tokenizer.countTokens()]. Then array[i] = tokenizer.nextToken() inside loop.
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.

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.