0

I am using Java 1.4 for an old project.

I have a string array

 String a[0]={"200,300"}
 String a[1]={"700,500"}
 String a[2]={"900,400"} 

as so on,

for this I need to create 2 string array to get the first value in a string array and second value in different string array.

ex:

String a[0]="200"
String a[1]="700"
String a[2]="900"
String b[0]="300"
String b[1]="500"
String b[2]="400"

I am not getting how can I do this.

asked Dec 6, 2013 at 5:39
5
  • what have u tried to solve it? Commented Dec 6, 2013 at 5:41
  • Hint : Assign array's 0th index data to a array and 1st index data to b. Also reconsider the array declaration for the string array you made... Commented Dec 6, 2013 at 5:45
  • 1
    I had a logic like I can create two string array where in first string array I can eliminate the first value where as in second one I will eliminate the value after ',' .but that code is not working properly. Commented Dec 6, 2013 at 5:50
  • 1
    @upadhyaykc take a look at my answer below. Commented Dec 6, 2013 at 6:24
  • 1
    @Prasad : The code is giving junk value for 2nd record. Commented Dec 6, 2013 at 6:45

3 Answers 3

3

If the length of the string array is known,

String[] newString = new String [6];
int count = 0;
for(int i = 0; i<3 ; i++){
 newString[i] = a[i];
 count++;
}
for(int j=0; j<3; j++){
 newString[count] = b[j];
 count++;
}

EDIT: Previously i got your question wrong. The following solution will help you. to divide a into 2 string arrays.

 String a[] = new String [3];
 a[0]={"200,300"};
 a[1]={"700,500"};
 a[2]={"900,400"};
 String[] newStr1 = new String [a.length];
 String[] newStr2 = new String [a.length];
 for(int i = 0; i<3 ; i++){
 String [] x= a[i].split(",");
 newStr1[i] = x[0];
 newStr2[i] = x[1];
 }
answered Dec 6, 2013 at 5:46
5
  • 2
    {200,300} on splitting this string you get {200 in x[0] to avoid 1st { i used substring() Commented Dec 6, 2013 at 6:42
  • 1
    @dbw: why have you deleted your post that gave me exact result. :) :) Commented Dec 6, 2013 at 6:47
  • 1
    @upadhyaykc I deleted because I thought the array elements are stored with brackets as told by Prasad Commented Dec 6, 2013 at 6:49
  • @upadhyaykc why you've removed tick from the answer BTW? Commented Dec 6, 2013 at 6:52
  • 1
    @prasad: Sorry Prasad,Because before editing it was giving junk value for 2nd record which I already mention in my previous comment. Commented Dec 6, 2013 at 7:11
2

Looking at your code, this provides you the correct solution,

int n = 3; //This can change as your array size is not known
String a[] = new String [n];
 a[0]="200,300";
 a[1]="700,500";
 a[2]="900,400"; //And So on
String[] b1 = new String [n];
String[] b2 = new String [n];
for(int i = 0; i<n ; i++)
{
 String [] numbers= a[i].split(",");
 b1[i] = numbers[0];
 b2[i] = numbers[1];
}
answered Dec 6, 2013 at 6:32
1

First you cant defined String array as string a[1]={700,500}. It is wrong way.

And second, Here is what you want :

a[0]=new String[]{"200","300"};
a[1]=new String[]{"700","500"};
a[2]=new String[]{"900","400"};
String x[] = new String[a.length];
String y[] = new String[a.length];
for(int i=0;i<a.length;i++){
 x[i] = a[i][0];
 y[i] = a[i][1];
}
answered Dec 6, 2013 at 5:46
3
  • 1
    it is given an error " the type of expression must be an array type but it resolved to string" . my code is : 'String []strId=null; strId=req.getParameterValues("Id");' Commented Dec 6, 2013 at 6:07
  • Define a like this : String a[][] = new String[3][2]; Commented Dec 6, 2013 at 6:08
  • 1
    since I am storing only one parameter value which is like '200,700' I can not use 2Darray. :( Commented Dec 6, 2013 at 6:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.