Results 1 to 8 of 8
Thread: Limiting loop in boolean string
- 10-10-2009, 05:04 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Limiting loop in boolean string
Here is my codes
How do I make it so if it read more than 3 e then it false.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; }
-
use an int counter variable, say call it eCounter, and count the number of e's.
- 10-10-2009, 05:51 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
So, i should not use string in this code?
-
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.
- 10-10-2009, 06:11 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
So I convert the string to charArray and try to count it, i still have problem
This is my errorJava 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;
Java Code:-------------------------------------------------------------------------------- X stringE("Hll")-> expected:<false> but was:<true> X stringE("")-> expected:<false> but was:<true> --------------------------------------------------------------------------------
-
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.
- 10-10-2009, 06:24 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
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.
-
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
-
String and while loop
By Exception in forum Java AppletsReplies: 5Last Post: 09-24-2009, 12:32 PM -
Using string to terminate loop
By mrblippy in forum New To JavaReplies: 3Last Post: 04-23-2009, 06:16 AM -
Entering a while loop with a not equal to string
By bri1547 in forum New To JavaReplies: 9Last Post: 07-09-2008, 07:10 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM -
terminating a while loop with a string
By tkdvipers in forum New To JavaReplies: 3Last Post: 07-09-2007, 11:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks