Results 1 to 8 of 8
  1. #1
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Limiting loop in boolean string

    Here is my codes
    Java Code:
        public boolean stringE(String str)
        {
            
            boolean result = false;
    
            int len = str.length();
            for (int i = 0; i < len; i++)
            {
            String word = str.substring(0);
            if (word.contains("e"))
            result = true;
          
    
            }
            return result;
        }
    How do I make it so if it read more than 3 e then it false.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    So, i should not use string in this code?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd convert the String to a char array via a String class method, that does just this, then iterate through each char in a for loop, checking to see if the char is an 'e' or not.

  5. #5
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    So I convert the string to charArray and try to count it, i still have problem

    Java Code:
     char[] letter = str.toCharArray();
            int[] count = new int[255];
    
            for(int i = 0; i < letter.length; i++) {
                  count[letter[i]]++;
                  if(count[letter[i]] > 3)
                    return false;
            }
            return true;
    This is my error

    Java Code:
    --------------------------------------------------------------------------------
      X stringE("Hll")-> expected:<false> but was:<true>
      X stringE("")-> expected:<false> but was:<true>
    --------------------------------------------------------------------------------

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please outline for those of us not in the know, just what the ultimate goal of this program is? To count 'e's? to count frequencies of all letters?

    Regardless, you will not be able to just return out of the for loop if a letter is encountered, but rather you'll probably need to loop through the entire string and depending on the results, choose the proper return boolean value.

  7. #7
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    I'm taking Intro to Java class and this is what I was given to do

    Java Code:
         *  Write the function stringE.
         *  
         *  @param
         *  @return
         *  
         *  Return true if the given string contains 
         *  between 1 and 3 'e' chars (inclusive).
    Last edited by jimmy-lin; 10-10-2009 at 06:27 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've often found that the key to figuring out the algorithm here is to do it on paper first.

    Get lots of paper, write several words, and step through the words figuring out if they'd be rated true or false, and in the mean time being very cogniscent of each step you take to make this determination. Then write down those steps in English, and finally translate to Java. It helps, trust me.

Similar Threads

  1. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  2. Using string to terminate loop
    By mrblippy in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 06:16 AM
  3. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 07:10 AM
  4. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM
  5. terminating a while loop with a string
    By tkdvipers in forum New To Java
    Replies: 3
    Last Post: 07-09-2007, 11:23 PM

Posting Permissions

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