Results 1 to 14 of 14
Like Tree6Likes
  • 1 Post By KevinWorkman
  • 1 Post By SurfMan
  • 1 Post By SurfMan
  • 2 Post By KevinWorkman
  • 1 Post By gimbal2

Thread: Picking out substrings

  1. #1
    based_walt is offline Member
    Join Date
    Aug 2013
    Location
    Germany
    Posts
    5
    Rep Power
    0

    Default Picking out substrings

    Hey,

    I just got into Java a couple weeks ago with Ivor Horton's Book, Beginnig Java, and I'm at chapter 4 now, where we learn about string operators and methods. In order to explain this, the author wrote a piece of code that was supposed to record the amount of "the" and "and" in a certain text. This is how the example goes:

    Java Code:
    public class FindCharacters {
      public static void main(String[] args) {
        // Text string to be analyzed
        String text = "To be or not to be, that is the question;"
                    + " Whether 'tis nobler in the mind to suffer"
                    + " the slings and arrows of outrageous fortune,"
                    + " or to take arms against a sea of troubles,"
                    + " and by opposing end them?";
    
        int andCount = 0;                  // Number of and's
        int theCount = 0;                  // Number of the's
    
        int index = -1;                    // Current index position
    
        String andStr = "and";             // Search substring
        String theStr = "the";             // Search substring
    
        // Search forwards for "and"
        index = text.indexOf(andStr);      // Find first 'and'
        while(index >= 0) {
          ++andCount;
          index += andStr.length();        // Step to position after last 'and'
          index = text.indexOf(andStr, index);
        }
    
        // Search backwards for "the"
        index = text.lastIndexOf(theStr);  // Find last 'the'
        while(index >= 0) {
          ++theCount;
          index -= theStr.length();        // Step to position before last 'the'
          index = text.lastIndexOf(theStr, index);
        }
        System.out.println("The text contains " + andCount + " ands\n"
                         + "The text contains " + theCount + " thes");
      }
    }
    As the author points out, the program indicates 5 "thes", because it also counts the ones in "wether" and "them". I'm just a rookie myself, but I tried writing a program that only counts "real" occurences of "the". Here's what I thought up (I limited the code to only search for "the", because there was no problem with counting "and" in the original):

    Java Code:
    public class FindingSubstrings {
        public static void main(String[] args){
            String text =   "To be or not to be, that is the question;"
                          + " Whether 'tis nobler in the mind to suffer"
                          + " the slings and arrows of outrageous fortune"
                          + " or to take arms against a sea of troubles"
                          + " and by opposing end them?";
            String andStr = "and";
            String theStr = "the";
            int theCount = 0;
            int index = -1;
            char ch, ch1;
            
            index = text.indexOf(theStr);
            while(index >= 0){
                ch = (char) (index-1);
                ch1 = (char) (index+3);
                if(Character.isWhitespace(ch) && Character.isWhitespace(ch1)){
                     ++theCount;
                }
                index += theStr.length();
                index = text.indexOf(theStr, index);
            }
            System.out.print("The text contains " + theCount + " thes. ");
        }
    }
    The program works like the original on, except that it searches for the character left of an instance of "the" (ch = index-1) and the one on the right, so basically the character next to the "e" in "the" (ch1 = index+3). Only if both characters are spaces can we be sure that the occurence is a real "the", because it's not attached to another word. The program doesn't work though: it always prints that there are 0 thes, but it should be three.

    Does anybody know why that is? This is my first programming book and I'm enjoying it a great deal so far, but this particular piece of code has been on my mind a lot lately. I just can't seem to find my mistake.
    Thanks in advance for any insight!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Picking out substrings

    Have you tried stepping through this with a debugger, or at least adding some print statements, to figure out what's going on?

    For example, are you sure you're capturing the space characters? How does your index move as your program executes?

    Also, why not just search for " the " with the spaces already included? Or you could use the String.split() function...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Picking out substrings

    Change line 16 and 17 to text.charAt(index-1) and text.charAt(index+3).

  4. #4
    based_walt is offline Member
    Join Date
    Aug 2013
    Location
    Germany
    Posts
    5
    Rep Power
    0

    Default Re: Picking out substrings

    Quote Originally Posted by KevinWorkman View Post
    Have you tried stepping through this with a debugger, or at least adding some print statements, to figure out what's going on?

    For example, are you sure you're capturing the space characters? How does your index move as your program executes?

    Also, why not just search for " the " with the spaces already included? Or you could use the String.split() function...
    Yes, I already tried defining theStr as " the " and it worked fine, but I was wondering whether there was an alternative. I'm guessing the index moves in the same way that it does in the original code (with every instance of "the") but the theCounter is only incremented when "the" is by itself. That was the idea anyway...
    String.split() hasn't been introduced in the book yet, so this is my first time hearing of it. I'll try it out though.

    Thanks!

  5. #5
    based_walt is offline Member
    Join Date
    Aug 2013
    Location
    Germany
    Posts
    5
    Rep Power
    0

    Default Re: Picking out substrings

    Quote Originally Posted by SurfMan View Post
    Change line 16 and 17 to text.charAt(index-1) and text.charAt(index+3).
    Awesome, it works perfectly that way. Thanks a lot!
    Why didn't it work with explicit casting as (char) though?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Picking out substrings

    As SurfMan said, and as I was trying to get you to think about without spoonfeeding the answer, you aren't ever referencing the String you're searching through. You're simply converting the indexes themselves to characters, which is meaningless.
    based_walt likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Picking out substrings

    Quote Originally Posted by based_walt View Post
    Awesome, it works perfectly that way. Thanks a lot!
    Why didn't it work with explicit casting as (char) though?
    You are casting the index itself, not the character AT the index...
    based_walt likes this.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Picking out substrings

    I always try not to spoonfeed, but I estimated the OP had put reasonable thought into it. As KevinWorkman suggest, I would highly recommend a debugger to step through each line of code to see what happens "under water". Are you coding this in Notepad™ or an IDE?

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Picking out substrings

    Yes but you see the outcome - you caught the man a fish, you did not teach him how to fish.

    But you're a good bloke anyway ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    based_walt is offline Member
    Join Date
    Aug 2013
    Location
    Germany
    Posts
    5
    Rep Power
    0

    Default Re: Picking out substrings

    I code in NetBeans, and I'll remember debugging in the future.

    Thanks for the explanation SurfMan and KevinWorkman, it makes perfect sense now. I wasn't aware of the difference between the index itself and the character referred to in that index, so it's no surprise that isWhitespace(ch) wasn't producing a reasonable result.

    I hope I'll learn how to fish in due time. ;)

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Picking out substrings

    I clearly differentiate between homework-dumping assholes and hard-working, studying people that can't find their mistake. I'll be the first to happily point out the mistakes :)
    based_walt likes this.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Picking out substrings

    Quote Originally Posted by SurfMan View Post
    I clearly differentiate between homework-dumping assholes and hard-working, studying people that can't find their mistake. I'll be the first to happily point out the mistakes :)
    Agreed. However, the problem is still the same- as Gimbal said, catching a man a fish instead of teaching him how to fish.

    The process of thinking through a problem and debugging unexpected behavior is more important than simply memorizing syntax, and it's impossible to teach without forcing people to do it themselves. This is especially important for simple programs like this, as it gives the student a chance to work through a basic problem without getting into anything complicated. That's what spoon-fed answers take away, and it's why many people have such a negative reaction to it. Only now you've taken that process away from somebody who actually wants to learn, which might even be worse.

    But it's a fine line, so I don't want to turn this into nitpicking over spoonfeeding. Your answer was a little more spoonfeedy than I was going for, but oh well. I'm sure the OP will have plenty of other chances to learn how to debug a problem. The problem is that now the book will start to get more complicated, and the OP still hasn't really had much practice debugging code, so his life will be more difficult as time goes on.

    But like I said I don't want to overly dramatize this and your intentions were good, so I say oh well.
    gimbal2 and SurfMan like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    based_walt is offline Member
    Join Date
    Aug 2013
    Location
    Germany
    Posts
    5
    Rep Power
    0

    Default Re: Picking out substrings

    Well, if it makes a difference, I really wasn't gonna arrive at a solution all by myself due to the misunderstanding about what an index actually is. Now that it has been explained, I get it and I also understood why the correction to my code had to be made for the program to function properly. So that's a bit of a learning experice in itself.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Picking out substrings

    Quote Originally Posted by based_walt View Post
    Well, if it makes a difference, I really wasn't gonna arrive at a solution all by myself due to the misunderstanding about what an index actually is. Now that it has been explained, I get it and I also understood why the correction to my code had to be made for the program to function properly. So that's a bit of a learning experice in itself.
    Indeed because you made the correct choice to not let it be but ask for a follow up; plenty of people would take the code fix and not think about it further, until the next time they run into the same or a similar problem and create forum post #2. So good for you.
    based_walt likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replacing words using substrings and indexOf
    By LetsG0Blue in forum New To Java
    Replies: 24
    Last Post: 03-02-2013, 03:15 PM
  2. Replies: 5
    Last Post: 04-09-2012, 09:14 PM
  3. help picking
    By gg4545 in forum New To Java
    Replies: 8
    Last Post: 04-05-2012, 07:57 AM
  4. Split string and joining substrings
    By KarlNorway in forum New To Java
    Replies: 28
    Last Post: 01-27-2012, 10:51 PM
  5. Replies: 9
    Last Post: 08-31-2010, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •