1

I am working on a String. e.g of string - Cam01,Cam02,Cam03

Actually I want to split the string using , and add to the String array of length 4.

since there are only 3 items , the last item must be added as string "False".

When i created String arr[] = s.split(","); it creates an array of 3 items. So last item i need to add as string "False" and make it array with length 4.

4
  • What problem are you having? Commented Jul 28, 2015 at 13:32
  • Please show us how you try to solve this problem. Commented Jul 28, 2015 at 13:35
  • 1
    possible duplicate of Resize an Array while keeping current elements in Java? Commented Jul 28, 2015 at 13:35
  • @user1235389 you might want to look at Arrays.copyOf()... Commented Jul 28, 2015 at 13:36

4 Answers 4

2

You can do this

String str = "Cam01,Cam02,Cam03";
str+= ",False";    
String[] arr = str.split(",");

now in your String array arr you have...

arr[0]=Cam01
arr[1]=Cam02
arr[2]=Cam03
arr[3]=False
Sign up to request clarification or add additional context in comments.

1 Comment

As far as I can tell, OP indicates that the data is in a consistent, expected format. If this is the case then this solution seems to be the simplest and shortest one presented
1

If you want to have an "Array" of variable size, I suggest using an ArrayList instead, where you can simply add things:

List<String> myArrayList = new ArrayList<String>( Arrays.toList( arr ) );

This way you will create a new ArrayList on which you can call "add":

while (myArrayList.size < 4) myArrayList.add("False"); }

If you really need an array later, you can call myArrayList.toArray( new String[4] );. But often it is better to work with Collections/Lists anyway.

Comments

0

The array created with the split function is an array of three elements.

It is not possible to change the size of an array once it is created.

So what you can do is create a new array of size 4. Copy all elements of the first array in this new array and set the value of fourth element of the new array to "false".

If you need to do that here is the code:

String[] splittedArray = s.split(",");
String[] newArray = new String[4];  // OR any size you like
System.arraycopy(splittedArray, 0, newArray, 0, splittedArray.length);
Arrays.fill(newArray, splittedArray.length, newArray.length, "False"); 

The newArray is the array of size four you are searching.

Note that the size of newArray must be greater then the size of the splittedArray.

Comments

0

You could do it like this with an buffer String array

String arr[] = new String[4];
String buffer[];
String string = "Cam01,Cam02,Cam03";
buffer = string.split(",");
// Forgot the copy...
System.arraycopy(buffer, 0, arr, 0, buffer.length);
for(int i = buffer.length;i<arr.length;++i) {
    arr[i] = "False";
}

1 Comment

@LanguidSquid why is it complicated? It fills the array with False after the last found value to the end of the array. Also why should it print three null values?

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.