简体   繁体   中英

Java android - How to split string data with empty data inside of it

I have a data string name stringSplit that I want to split into 5 parts.

I use this to split the String:

String[] pisah = stringSplit.split(",", -1);
for (int i=0;i< pisah.length;i++) {
   String hasil1 = pisah[0];
   String hasil2 = pisah[1];
   String hasil3 = pisah[2];
   String hasil4 = pisah[3];
   String hasil5 = pisah[4];
}

When the value in stringSplit there are 5 data (fe: “data1,data2,data3,data4,data5”), the code is working properly. But when in stringSplit there are only one, two or three data, an exception will caught:

(f.e: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4).

And my question is how to handle the empty string? So even though the data only one, two, or three data the stringSplit still can be splitted.

I've tried by adding limit to -1 which this solution I got from another post, but still not working for me.

Editted:

I almost forgot, the result string (hasil1 - hasil5) will use for edittext.

fe: editText1.setText(hasil1);

and others.

Here is the problem,

for (int i=0;i< pisah.length;i++) {
   String hasil1 = pisah[0];
   String hasil2 = pisah[1];
   String hasil3 = pisah[2];
   String hasil4 = pisah[4];
   String hasil5 = pisah[5];
}

but that iterates over the list and gets the values in the list from 0-5 every time it loops, so what you should be doing is something like this

for(int i = 0; i < 6; i++){
    if(i >= pisah.length()) break;  
    String hasilI = pisah[i];
}

This iterates over the list 6 times then if the list's size < 6 it will break out of the loop

One possible solution is to check if the index you are looking for is less than the length of the array. If that index is less than the size of the array than it definitely exists.

for (int i=0;i< pisah.length;i++) {
  if(0 < pisah.length)
     String hasil1 = pisah[0];
  if(1 < pisah.length)
     String hasil2 = pisah[1];
  //similarly for all other idexes that you want the value for.
}

use this code:

 String res[] = new String[5];

String[] pisah = stringSplit.split(",");

for (int i=0;i< pisah.length && i < res.length;i++) {
  res[i] = pisah[i];
}

if(res[0] != null )
  //.......... res[0] valid 

if(res[1] != null )
  //.......... res[1] valid 

if(res[2] != null )
  //.......... res[2] valid 

if(res[3] != null )
  //.......... res[3] valid 

if(res[4] != null )
  //.......... res[4] valid 

You have hard coded the number of EditText fields you can have! What if, there is a change in requirement, where you have to add another field? That would need you to add "editText6" and "hasil6" at a lot of places. The code is not nimble footed to changes in specs.

This is what I would do.

  1. Create a list of EditText, say, textList
  2. Iterate over the pisah array, and textist simultaneously and add the String at index i in pisah to the EditText object at index i in textList.

This way, in case there are only 3 data values, you are automatically taking care of populating only 3 EditText objects.

This is the code :

List<EditText> textList;
String[] pisah = stringSplit.split(",", -1);
for (int i=0;i< pisah.length;i++) {
           textList.get(i).setTest(pisah[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM