7 Replies Latest reply: Feb 27, 2010 12:47 PM by 843789 RSS

    while loop with substring()

    843789
      Hi,

      been having some problems with a while look trying to extract parts of a string. Basically I want it to display each word on a separate line from a string. Here's what I have so far:
      public class sString {
           public static void main(String[] args){
      
                String pLine = "I love learning Java programming";
                int i = 0;
           
                while (i < pLine.length()){
                     System.out.println(pLine.substring(i,(pLine.indexOf(" ", i))));
                     i = (pLine.indexOf(" ", i));
           }
      
           }
      } 
      Edited by: user_invalid on Feb 27, 2010 3:44 PM
        • 1. Re: while loop with substring()
          843789
          Use String.split instead:
          public class SString {
            public static void main(String[] args) {
          
              String pLine = "I love learning Java programming";
              //int i = 0;
          
              String[] tokens = pLine.split(" ");
              for (String token : tokens) {
                System.out.println(token);
              }
              System.out.println();
              // while (i < pLine.length()) {
              // System.out.println(pLine.substring(i, (pLine.indexOf(" ", i))));
              // i = (pLine.indexOf(" ", i));
              // }
          
            }
          }
          Incidentally, your use of indexOf is wrong. Check out the String API to see why.

          Edited by: Encephalopathic on Feb 27, 2010 7:49 AM
          • 2. Re: while loop with substring()
            843789
            Thanks, is it possible to do it with substring() and indexOf() though?
            • 3. Re: while loop with substring()
              843789
              user_invalid wrote:
              Thanks, is it possible to do it with substring() and indexOf() though?
              You bet, but you have to correct your use of indexOf, and in particular the second parameter to this method. It's not the index count that matters but the starting location in the String that matters.
              • 4. Re: while loop with substring()
                843789
                I'm not too sure where I'm going wrong... the correct use of indexOf is: indexOf(int ch, int startIndex) so by using the variable 'i' isn't this serving as the position in the string in which to start from?
                • 5. Re: while loop with substring()
                  843789
                  user_invalid wrote:
                  I'm not too sure where I'm going wrong... the correct use of indexOf is: indexOf(int ch, int startIndex) so by using the variable 'i' isn't this serving as the position in the string in which to start from?
                  startIndex is the position to start looking in the string and has nothing to do with 'i'.
                  • 6. Re: while loop with substring()
                    843789
                    Couldn't you use a variable to change the startIndex position though, e.g I was trying to set the startIndex to the white space before the last word
                    • 7. Re: while loop with substring()
                      843789
                      My mistake. Yes, you're on the right track, but I believe that indexOf returns -1 if no match found. i.e.,
                          String searchStr = " ";
                          int first = pLine.indexOf(searchStr, 0);
                          int second = 0;
                          
                          while (first != -1) {
                            first++;
                            second = pLine.indexOf(searchStr, first);
                            if (second != -1) {
                              System.out.println(pLine.substring(first, second));
                            }
                            else {
                              System.out.println(pLine.substring(first));
                            }
                            first = second;
                          }