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.

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];

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

5 Comments

{200,300} on splitting this string you get {200 in x[0] to avoid 1st { i used substring()
@dbw: why have you deleted your post that gave me exact result. :) :)
@upadhyaykc I deleted because I thought the array elements are stored with brackets as told by Prasad
@upadhyaykc why you've removed tick from the answer BTW?
@prasad: Sorry Prasad,Because before editing it was giving junk value for 2nd record which I already mention in my previous comment.
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];
}

Comments

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];
}

3 Comments

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");'
Define a like this : String a[][] = new String[3][2];
since I am storing only one parameter value which is like '200,700' I can not use 2Darray. :(

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.