Sign in to follow this  

Need help regarding loops - using Java

This topic is 3572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'll just jump straight into it. Here's what the code looks like (I won't post all of the code because that would be redundant): System.out.print("What is your name?: "); enteredString = Keyboard.readString(); if (enteredString.equals("Metallon")) { System.out.println("Bye."); } } else { System.out.println("What is your name?: "); } So what I want to do is to have the question "What is your name?" loop until I answer "Metallon". I would do this using 'do while'. do { //whater code I want in here } while (//any condition I want); So while the loop works, I don't know what condition to use that allows me to move on when answering correctly "Metallon". Another minor issue I'm having is an if statement I want. I want to have two statements. What do I use to tell the computer I want to have two statements? Like... if X and Y { //code here } I started learning Java yesterday, and by then I only could write a Hello World program. I don't have a book, either, so until I get ahold of one, I'm kind of stuck (need to buy one). Thanks in advance!

Share this post


Link to post
Share on other sites
Quote:
Original post by Metallon
I'll just jump straight into it. Here's what the code looks like (I won't post all of the code because that would be redundant):


System.out.print("What is your name?: ");
enteredString = Keyboard.readString();

if (enteredString.equals("Metallon"))
{
System.out.println("Bye."); }
}

do
{
//whater code I want in here
}
while (//any condition I want);


Another minor issue I'm having is an if statement I want. I want to have two statements. What do I use to tell the computer I want to have two statements? Like...

if X and Y
{
//code here
}


the while will repeat as long as it is true. Currently if you put the condition from your if statement in the while loop it would do the exact opposite of what you want. ! is the logical negation operator. && is logical and || is logical or.

Share this post


Link to post
Share on other sites
Quote:

the while will repeat as long as it is true. Currently if you put the condition from your if statement in the while loop it would do the exact opposite of what you want. ! is the logical negation operator. && is logical and || is logical or.


I know how while works, and the issue lies there. I want the following condition to be that the entered word(s) is NOT "Metallon". In other words, I need the not-equivalent of stringex.equals(//stuff in here); where stringex is what I named the string... thing.

As for the operators, I knew it was && gah but for some reason it doesn't want to work. I might've missed something, but Java is only telling me that it's a Syntax error, and that it's 'invalid OnlySynchronized'

Share this post


Link to post
Share on other sites
Quote:
Original post by Metallon


I know how while works, and the issue lies there. I want the following condition to be that the entered word(s) is NOT "Metallon". In other words, I need the not-equivalent of stringex.equals(//stuff in here); where stringex is what I named the string... thing.

As for the operators, I knew it was && gah but for some reason it doesn't want to work. I might've missed something, but Java is only telling me that it's a Syntax error, and that it's 'invalid OnlySynchronized'




while(!enteredString.equals("Metallon"))
reads while not enteredString equals "Metallon"

Java is very strict about what can be in a condition for an if statement or a while statement it must evaluate to a bool. Please share the statement that is giving you trouble.
you should be able to do
if(x && y)
{
dostuff();
dostuff2();
}

Share this post


Link to post
Share on other sites
All problems solved. The problem with my two statements in the boolean thing was due to my putting out parentheses at slightly wrong places.

Thank you for your time and for your help: very much appreciated!

Share this post


Link to post
Share on other sites
I'm a little bit rusty in Java but can't you do something like this:

System.out.print("What is your name?: ");

do
{
enteredString = Keyboard.readString();
if (enteredString.equals("Metallon"))
{
break;
}
else
{
System.out.println("What is your name?: ");
}

}
while (true);

System.out.println("Bye.");

Share this post


Link to post
Share on other sites

This topic is 3572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this