1

I'm working with string value like this String result "aa,bb,cc,dd,ee".

And already split it into aa bb cc dd ee with

qrList = result.getContents().toString().split("\\,");
List<String> resultToString= new ArrayList(Arrays.asList(qrList));

Then I create four ArrayLists.

 ArrayList<String> strA = new ArrayList();
 ArrayList<String> strB = new ArrayList();
 ArrayList<String> strCD = new ArrayList();
 ArrayList<String> strE = new ArrayList();

When I use this code below to store string into each new ArrayList.

for(int count=0; count<resultToString.size(); count++){
    //separate string to array list
    if(count%5==0){
        strA.add(resultToString.get(count));
    }else if(count%5==1){
        strB.add(resultToString.get(count));
    }else if(count%5==2||count%5==3){
        strCD.add(resultToString.get(count));
    }else if(count==4){
        strE.add(resultToString.get(count));
    }

The correct result would be

  • strA stored aa
  • strB stored bb
  • strCD stored cc and dd
  • strE stored ee

It doesn't work because I only get the index value at 0 (strA stored aa). What should I do to improve my code?

4
  • 2
    It would be great if you format your code first Commented Jun 8, 2017 at 10:56
  • Do you split work fine? I tried it manually like "aa,bb,cc,dd,ee".toString().split("\\,"); and then you code works fine. Commented Jun 8, 2017 at 11:03
  • What you said is not possible. count==4 must be true for your given input Commented Jun 8, 2017 at 11:04
  • To motis10,the split method works fine. Commented Jun 9, 2017 at 4:08

2 Answers 2

2

Simply use startsWith and add appropriate values to list

    for(int count=0; count<resultToString.size(); count++){
        //separate string to array list
        String s = resultToString.get(count);

        if(s.startsWith("a")){
            strA.add(s);
        }else if(s.startsWith("b")){
            strB.add(s);
        }else if(s.startsWith("c")||s.startsWith("d")){
            strCD.add(s);
        }else if(s.startsWith("e")){
            strE.add(s);
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Instead of giving an alternate solution please explain why his code isn't working.
@earthw0rmjim by OP What should I do to improve my code
I think the strings are arbitrary
@earthw0rmjim i couldn't find anything wrong so i went for testing and nothing wrong again , it must be something else wrong somewhere , though i suggested a bit clear approached
0

Modulus operator describes the remainder of your equation. Let us show and explain an example:

As explained in this documentation, performing modulus between two values will only show you the remainder value. For e.g. 8 % 3 == 2 because 8 divided by 3 leaves a remainder of 2.

The reason your code is not showing your desired results is due to the usage of your modulus operator. Regarding improving your code, I can tell that all you wanted is to use the Array List index so as to add the string from the resultToString Array List.

Therefore I would modify your loop in the following manner:

for(int count=0; count < resultToString.size(); count++) {
  //separate string to array list
  if (count == 0){
     strA.add(resultToString.get(count));
  } else if (count == 1){
     strB.add(resultToString.get(count));
  } else if (count == 2 || count == 3){
     strCD.add(resultToString.get(count));
  } else if (count == 4){
     strE.add(resultToString.get(count));
  }
}

An alternative is to loop directly on the resulting String[] and use that relative counter in the for loop for e.g.

String[] qrList = results.getContent().toString().split("\\,");

for(int count = 0; count < qrList.length; count++) {
  //each index represents the split string e.g
  // [0] == "aa"
  // [1] == "bb"
  // [2] == "cc"
  // [3] == "dd"
  // [4] == "ee"
  if (count == 0){
     strA.add(qrList[count]);
  } else if (count == 1){
     strB.add(qrList[count]);
  } else if (count == 2 || count == 3){
     strCD.add(qrList[count]);
  } else if (count == 4){
     strE.add(qrList[count]);
  }
}

1 Comment

To Koshux,When I use your code to test, it can't store any value in the ArrayList.

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.