Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A Beginner:Why is my program always returning true?

Reply
Thread Tools

A Beginner:Why is my program always returning true?

 
 
Enteng
Guest
Posts: n/a
 
      11-19-2007
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!
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      11-19-2007
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!


class findInString {
public static void main(String[] args){
String mainString = "The quick brown fox jumped over the lazy
dog.";
String subString = "hey";
boolean isItThere = mainString.contains(subString);
System.out.println(isItThere ? "Word found" : "Not found.");
}
}

Arne
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      11-19-2007

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)
{
System.out.print(args[0].indexOf(args[1]));
}
}
>
>
> If you guys see anything that I should improve on (my programming)
> please feel free to comment. I'd appreciate them. Thanks!



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-19-2007
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.

....

If the objective is to find out whether the substring is in the string,
just call the String method "contains".

However, if the objective is to learn more about programming, you should
debug this one.

I suggest using much shorter test strings. That way, you can work
through your code with paper and pencil.

Patricia
 
Reply With Quote
 
Enteng
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 12:08 pm, Patricia Shanahan <p...@acm.org> wrote:
> 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.

>
> ...
>
> If the objective is to find out whether the substring is in the string,
> just call the String method "contains".
>
> However, if the objective is to learn more about programming, you should
> debug this one.
>
> I suggest using much shorter test strings. That way, you can work
> through your code with paper and pencil.
>
> Patricia


Actually yeah, the objective is to learn more about programming.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-19-2007
On Sun, 18 Nov 2007 18:55:23 -0800 (PST), Enteng <>
wrote, quoted or indirectly quoted someone who said :

>while (subStringSize-- > 0){
> if (mainString.charAt(mainStringCounter) !=
>subString.charAt(subStringCounter)){
> continue Test;
> }


A few thoughts.

1. "continue" is almost never used in Java. You can usually get rid
of it by inverting the sense of your condition.

2. A loop must have something change each time around that has bearing
on when we quit. In this case what is changing? What are you TRYING to
compare with what?

3. code is easier to understand if you put "final" on any variable
that will not change its value later.

4. code is usually easier to understand if anything that is computed
in a loop that would give the same result every time around, should be
pulled out ahead of the loop and assigned to a final variable. The
simpler the innermost loop is, the easier the code is to understand
and the faster it will run.

5. Your code is missing the comments explaining what it is TRYING to
do. Without that, there is no way fix it.

6. if the assumption others made about "contains" is true, try your
code out with pencil and paper, or peppered with printouts to follow
it through step by step on a concrete example to see how it goes off
the rails.

I am being deliberately vague. I figure you will learn more from just
some hints that having the solution handed on a plate. I also get the
feeling you would rather sweat a bit.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Curt Welch
Guest
Posts: n/a
 
      11-19-2007
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/
 
Reply With Quote
 
Enteng
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 2:33 pm, c...@kcwc.com (Curt Welch) wrote:
> Enteng <ente...@gmail.com> 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/
> c...@kcwc.com http://NewsReader.Com/


Thanks for the comments and suggestions. I'm learning a lot
I thought giving variable detailed names would help me understand the
code better. Anyway I think I got it.

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 i = 0; i < max; i++){
int cnt = subString.length();
int s = 0;
int m = i;

while (cnt-- != 0){
if (mainString.charAt(m++) != subString.charAt(s++)){
continue Test;
}
}

isItThere = true;
break Test;
}

System.out.println(isItThere ? "Word found" : "Not found.");
}
}
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-19-2007
Enteng wrote:
> Thanks for the comments and suggestions. I'm learning a lot
> I thought giving variable detailed names would help me understand the
> code better. Anyway I think I got it.
>
> class findInString {


Class names should begin with an upper-case letter.

> public static void main(String[] args){


Do NOT use TAB characters in Usenet listings; use spaces to indent (2, 3, or 4
spaces per level).

> String mainString = "The quick brown fox jumped over the lazy dog.";


// "String" is not a good name part for variables.
// It ties the variable to the physical implementation too tightly.

> String subString = "hey";
> boolean isItThere = false;


> Test:

/*
Don't use labels to continue loops. 'continue' by itself is enough, or
better, follow Roedy's suggestion and invert the condition to eliminate the
'continue'. (And if you ask for advice, you should consider it before
rejecting it.)
*/

> for (int i = 0, max = mainString.length(); i < max; i++){
> int s = 0;
> int m = i;
>

for ( int cnt = subString.length(); cnt-- != 0 && ! isItThere; ){
if (mainString.charAt(m++) == subString.charAt(s++)){
isItThere = true;
> }
> }
> }
>
> System.out.println(isItThere ? "Word found" : "Not found.");
> }
> }


--
Lew
 
Reply With Quote
 
Enteng
Guest
Posts: n/a
 
      11-19-2007
On Nov 19, 11:33 pm, Lew <l...@lewscanon.com> wrote:
> Enteng wrote:
> > Thanks for the comments and suggestions. I'm learning a lot
> > I thought giving variable detailed names would help me understand the
> > code better. Anyway I think I got it.

>
> > class findInString {

>
> Class names should begin with an upper-case letter.
>
> > public static void main(String[] args){

>
> Do NOT use TAB characters in Usenet listings; use spaces to indent (2, 3, or 4
> spaces per level).
>
> > String mainString = "The quick brown fox jumped over the lazy dog.";

>
> // "String" is not a good name part for variables.
> // It ties the variable to the physical implementation too tightly.
>
> > String subString = "hey";
> > boolean isItThere = false;
> > Test:

>
> /*
> Don't use labels to continue loops. 'continue' by itself is enough, or
> better, follow Roedy's suggestion and invert the condition to eliminate the
> 'continue'. (And if you ask for advice, you should consider it before
> rejecting it.)
> */
>
> > for (int i = 0, max = mainString.length(); i < max; i++){
> > int s = 0;
> > int m = i;

>
> for ( int cnt = subString.length(); cnt-- != 0 && ! isItThere; ){
> if (mainString.charAt(m++) == subString.charAt(s++)){
> isItThere = true;
>
> > }
> > }
> > }

>
> > System.out.println(isItThere ? "Word found" : "Not found.");
> > }
> > }

>
> --
> Lew



OK thanks I'll take note of that. About Roedy's suggestion I just
haven't tried some of it yet because I'm making this code while I'm on
the topic of labeled breaks (if that's what you're pertaining to about
rejecting advice). Actually it was one of his advices that made me
figure this out Lew, you sound offended or it's just the way you
give advice?

Honestly speaking it's kind of hard being criticized. I'm saying this
not because of your comments and suggestions(which are actually
helpful and I'm thankful for) but it's just that some comments are
well, discouraging. Don't get me wrong I appreciate all of them. Maybe
I'm just not used to this. Anyway I'm OT now, just voicing out

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList is always returning the value from index 0 Nathan Sokalski ASP .Net 2 09-04-2005 11:28 PM
stdin/stdout fileno() always returning -1 from windows service chuck Python 4 07-18-2005 10:09 PM
DropDownList always returning the first item Siamak Zahedi ASP .Net 3 01-28-2005 02:08 PM
'exit function'...always use it when returning a value? darrel ASP .Net 3 08-06-2004 07:02 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57