Author |
Message |
avsrule247
|
Posted: Mon Feb 12, 2007 6:53 pm Post subject: Need to exit a loop |
|
|
My program gets input from the user. It checks to see whether the input first letter is between A or H, I or P, or Q and Z. That works fine. But when the user enters the word "stop", the program is supposed to stop. What is the problem?
Java: |
import java.io.*;
public class example10
{
public static void main (String [] args ) throws IOException
{
BufferedReader input = new BufferedReader (new InputStreamReader (System. in));
String strinput;
int intqTz = 0;
int intaTh = 0;
int intiTp = 0;
final String strEND = "stop";
System. out. println ("Enter " + strEND + " to terminate.");
while (true)
{
strinput = input. readLine ();
//THIS IS WHERE IT IS SUPPOSED TO EXIT
if (strinput == "stop")
{
break;
}
while (strinput != strEND )
{
if (strinput. compareTo ("i") < 0)
{
intaTh = intaTh + 1;
break;
}
else if ((strinput. compareTo ("h") > 0) && (strinput. compareTo ("q") < 0))
{
intiTp = intiTp + 1;
break;
}
else if (strinput. compareTo ("p") > 0)
{
intqTz = intqTz + 1;
break;
}
else
{
System. out. println ("Invalid Name");
break;
}
}
}
System. out. println ("There were " + intaTh + " A to H names.");
System. out. println ("There were " + intiTp + " I to P names.");
System. out. println ("There were " + intqTz + " Q to Z names.");
}
}
|
Please keep in mind that I am totally new to java, and have just learn't the basics. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Feb 12, 2007 6:58 pm Post subject: RE:Need to exit a loop |
|
|
What does == do for objects? |
|
|
|
|
|
avsrule247
|
Posted: Mon Feb 12, 2007 7:02 pm Post subject: RE:Need to exit a loop |
|
|
Ohhh yes! I must use compareTo.
Thanks for your help. I knew it was just a simple mistake that I made. |
|
|
|
|
|
wtd
|
Posted: Mon Feb 12, 2007 7:51 pm Post subject: RE:Need to exit a loop |
|
|
Are you sure compareTo is the best answer?
Are there any other methods that might work better? |
|
|
|
|
|
avsrule247
|
Posted: Mon Feb 12, 2007 9:09 pm Post subject: Re: Need to exit a loop |
|
|
Well, I only know of compareTo, since I just started to learn java 4 days ago. I'm sure there are better ways though.
But if you have any suggestions I'd be more then happy to attempt to use them. |
|
|
|
|
|
wtd
|
Posted: Mon Feb 12, 2007 9:12 pm Post subject: RE:Need to exit a loop |
|
|
Well, you're checking for _____, which sounds like the name of which method defined for all Objects?
I'm not just torturing you here. I'm just trying not to give out easy answers. |
|
|
|
|
|
avsrule247
|
Posted: Mon Feb 12, 2007 9:35 pm Post subject: RE:Need to exit a loop |
|
|
Well, let's see...
I am checking the value of the string? A variable? The ascii codes of the first letter? I have no idea. All I know is that I am sorting.
You are very good at torturing. Although I don't blame you, I would have done the same thing. |
|
|
|
|
|
ericfourfour
|
Posted: Mon Feb 12, 2007 10:54 pm Post subject: RE:Need to exit a loop |
|
|
*cough* equality *cough* |
|
|
|
|
|
Sponsor Sponsor
|
|
|
avsrule247
|
Posted: Tue Feb 13, 2007 12:10 pm Post subject: Re: Need to exit a loop |
|
|
Yeah, that mite work. It's just too bad that I don't know how to use it. |
|
|
|
|
|
ericfourfour
|
Posted: Tue Feb 13, 2007 4:45 pm Post subject: RE:Need to exit a loop |
|
|
google: "java Object" (or even better "java String")
The javadoc will be the first link. Look for the method used to determine equality between objects. |
|
|
|
|
|
|