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.
-
what have u tried to solve it?Zaheer Ahmed– Zaheer Ahmed2013年12月06日 05:41:36 +00:00Commented 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...AurA– AurA2013年12月06日 05:45:43 +00:00Commented Dec 6, 2013 at 5:45
-
1I 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.Sunil– Sunil2013年12月06日 05:50:37 +00:00Commented Dec 6, 2013 at 5:50
-
1@upadhyaykc take a look at my answer below.Prasad– Prasad2013年12月06日 06:24:52 +00:00Commented Dec 6, 2013 at 6:24
-
1@Prasad : The code is giving junk value for 2nd record.Sunil– Sunil2013年12月06日 06:45:22 +00:00Commented Dec 6, 2013 at 6:45
3 Answers 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];
}
-
2
{200,300}
on splitting this string you get {200 inx[0]
to avoid 1st { i usedsubstring()
Prasad– Prasad2013年12月06日 06:42:31 +00:00Commented Dec 6, 2013 at 6:42 -
1@dbw: why have you deleted your post that gave me exact result. :) :)Sunil– Sunil2013年12月06日 06:47:03 +00:00Commented Dec 6, 2013 at 6:47
-
1@upadhyaykc I deleted because I thought the array elements are stored with brackets as told by PrasadDeepak Bhatia– Deepak Bhatia2013年12月06日 06:49:21 +00:00Commented Dec 6, 2013 at 6:49
-
@upadhyaykc why you've removed tick from the answer BTW?Prasad– Prasad2013年12月06日 06:52:10 +00:00Commented 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.Sunil– Sunil2013年12月06日 07:11:24 +00:00Commented Dec 6, 2013 at 7:11
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];
}
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];
}
-
1it 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");'Sunil– Sunil2013年12月06日 06:07:22 +00:00Commented Dec 6, 2013 at 6:07
-
Define
a
like this :String a[][] = new String[3][2];
Vimal Bera– Vimal Bera2013年12月06日 06:08:41 +00:00Commented Dec 6, 2013 at 6:08 -
1since I am storing only one parameter value which is like '200,700' I can not use 2Darray. :(Sunil– Sunil2013年12月06日 06:16:09 +00:00Commented Dec 6, 2013 at 6:16