Enteng <> wrote:
> Hi I'm learning Java and there seems to be something wrong with my
> program. It's supposed to identify if the substring is in the main
> string. My program always returns true and I'm a bit confused.
>
> BTW, this is based from the tutorial from sun. I've read the whole
> fundamentals section and try to re-write the examples there after a
> while.
>
> Anyway here's the code:
>
> class findInString {
> public static void main(String[] args){
> String mainString = "The quick brown fox jumped over the
> lazy dog."; String subString = "hey";
> boolean isItThere = false;
>
> int max = mainString.length();
> Test:
> for (int letter = 0; letter < max; letter++){
> int subStringSize = subString.length();
> int subStringCounter = 0;
> int mainStringCounter = letter;
>
> while (subStringSize-- > 0){
> if (mainString.charAt(mainStringCounter)
> != subString.charAt(subStringCounter)){
> continue Test;
> }
> }
>
> isItThere = true;
> break Test;
> }
>
> System.out.println(isItThere ? "Word found" : "Not
> found."); }
> }
>
> If you guys see anything that I should improve on (my programming)
> please feel free to comment. I'd appreciate them. Thanks!
As others have said, you just have a bug in the code. We all make mistakes
when we write code, and the one most important thing to learn, is how to
find bugs without help from others. Half of what a programmer must do is
remove bugs from code. There is nothing odd happening with the language,
your code simply isn't doing what you want it to do because you made a
simple mistake.
When code doesn't do what you expect it to do, look at it carefully and
think about what it's doing and you will normally be able to find the
problem.
You might try working backwards for example. If the program prints out
"Word found", then what does that tell you? It tells you that isItThere
must be true. So work backwards from that. How can isItThere be true?
There's only one place in the code it gets set to true, so you know it must
have been set there. Keep working backwards to see how that could have
happened.
When in doubt, add println() for every step of the code and look at the
100's of lines of output and see exactly what it's doing. That will allow
you to be sure all the statements in the code are doing what you expect
them to do, and it will allow you to see why the program is returning true.
You actually have 3 bugs in your code. If you fix the first two, you will
find the third rather quickly.
As far as general comments, here are a few to think about.
Try and keep the code as simple, and as concise as possible. Don't use
variables when you don't really need them, and use as short of name as is
reasonable for the scope of the variables. Variables with very limited
scope (like the index in a short loop) should have very short names, like 1
to 3 characters, local variables in a short method might be 3 to 5 letters
long. Arguments a big longer, and instance variables even longer.
If the variable names are too long, it makes it harder to "see" the code in
the middle of all the long and verbose variables. Here's a rewrite of your
code without changing the logic using shorter names. Maybe it will help
you see the error? I would suggest other changes, but I don't want to give
away what you bugs are until you find them on your own.
class findInString {
public static void main(String[] args){
String main = "The quick brown fox jumped over the lazy dog.";
String sub = "hey";
boolean found = false;
next:
for (int i = 0; i < main.length(); i++) {
int cnt = sub.length();
int s = 0;
int m = i;
while (cnt-- > 0) {
if (main.charAt(m) != sub.charAt(s))
continue next;
}
found = true;
break;
}
System.out.println(found ? "Word found" : "Not found.");
}
}
--
Curt Welch
http://CurtWelch.Com/
http://NewsReader.Com/