12 Replies Latest reply: Mar 13, 2013 1:02 AM by 800839 RSS

    Splitted Data to be Set in the respective list

    800839
      Hi,

      I want to set the respective spilted values in the respective List, but since the length is coming as 2 only in the firstList both the splitted values are populated.
       String test = "900000005146$@$AR@L-";
              List<String> firstList = new ArrayList<String>();
              List<String> secondList = new ArrayList<String>();
              String[] parts = test.split("\\$@\\$");
                  if(parts != null){
                      for(int i = 0; i < parts.length; i++){
                        System.out.println(parts[0]);
                        firstList.add(parts[0]);
                        System.out.println("FirstList:"+firstList);
                        if (parts.length >= 2) {
                            secondList.add(parts[1]);
                            System.out.println("FirstList:"+secondList);
                        }
                      }
                    } else {
                      System.out.println("Failed!");
                    }
      Output:-
      -----------

      900000005146
      FirstList:[900000005146]
      FirstList:[AR@L-]
      900000005146
      FirstList:[900000005146, 900000005146]
      FirstList:[AR@L-, AR@L-]

      Instead I want the data to be populated as below:-

      900000005146
      FirstList:[900000005146]
      AR@L-
      SecondtList:[AR@L-]

      Please clarify.

      Thanks.

      Edited by: 797836 on Mar 12, 2013 7:05 AM
        • 1. Re: Splitted Data to be Set in the respective list
          gimbal2
          Please, use \
           tags when posting code. As you can see the forum is breaking your code because it is trying to interpret the characters.                                                                                                                                                                                                                                                                                        
          • 2. Re: Splitted Data to be Set in the respective list
            800839
            Thanks gimbal2, I will follow that. Please clarify the question.

            THANKS.
            • 3. Re: Splitted Data to be Set in the respective list
              Kayaman
              797836 wrote:
              Thanks gimbal2, I will follow that.
              Do it now. Edit your post and format the code.
              • 4. Re: Splitted Data to be Set in the respective list
                800839
                I have added
                 tags , please clarify ?                                                                                                                                                                                                                        
                • 5. Re: Splitted Data to be Set in the respective list
                  Kayaman
                  Well, you're adding both parts in the for-loop. Since you loop twice, you're getting duplicates.
                  • 6. Re: Splitted Data to be Set in the respective list
                    800839
                    Thanks. How we can ensure that the second splitted value is stored in the secondList?
                    • 7. Re: Splitted Data to be Set in the respective list
                      Kayaman
                      797836 wrote:
                      Thanks. How we can ensure that the second splitted value is stored in the secondList?
                      It is being stored in the second list. If you paid attention to your code, you'd notice you're printing out "First list" + secondList.
                      • 8. Re: Splitted Data to be Set in the respective list
                        800839
                        Yes the secondList contains the respective data, but the firstList also hold it, since the length is 2 both the data are stored in the firstList? How we can ensure only the first splitted data is stored in the firstList and second splitted data is stored in the secondList. Please help me as this is bit urgent.

                        Thanks
                        • 9. Re: Splitted Data to be Set in the respective list
                          Kayaman
                          797836 wrote:
                          Yes the secondList contains the respective data, but the firstList also hold it
                          No it doesn't. The first list contains "900000005146" and the second list contains "AR@L-". Then you duplicate the data because of your useless for-loop.
                          since the length is 2 both the data are stored in the firstList?
                          Jeez. You're making it really hard to help you. The length is 2 because you're adding the data to both lists in a loop. 2x2 = 4, 2 items per list.

                          Please help me as this is bit urgent.
                          What is this for? It doesn't look like homework.
                          • 10. Re: Splitted Data to be Set in the respective list
                            gimbal2
                            797836 wrote:
                            Please help me as this is bit urgent.
                            That's your problem - you're letting your sense of urgency block you from understanding the help people are giving you. Apparently you are completely blind to it, seeing as you keep asking for help even when you are getting it.

                            Or were you expecting people to hand over corrected code?
                            • 11. Re: Splitted Data to be Set in the respective list
                              800839
                              Thanks Guys.

                              String test = "900000005146$@$AR@L-";
                                      List<String> firstList = new ArrayList<String>();
                                      List<String> secondList = new ArrayList<String>();
                                      String[] parts = test.split("\\$@\\$");
                                      System.out.println("the length is:"+parts.length);
                                          if(parts != null){
                                              for(int i = 0; i < parts.length; i++){
                                                firstList.add(parts[0]);
                                                System.out.println("FirstList:"+firstList);
                                                if (parts.length >= 2) {
                                                    secondList.add(parts[1]);
                                                    System.out.println("SecondList:"+secondList);
                                                    break;
                                                }
                                              }
                                            } else {
                                              System.out.println("Failed!");
                                            }
                              Added the break; to get the correct results.

                              Thanks.
                              • 12. Re: Splitted Data to be Set in the respective list
                                Kayaman
                                797836 wrote:
                                Added the break; to get the correct results.
                                That is a rather stupid solution. You should remove the for-loop instead, since you're not using it.

                                And please, go through some tutorials. You're seriously lacking in the basics.