File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops" Watch "Loops" New topic


macca Mason

Joined: Nov 26, 2003
Posts: 13
Hi I have a loop that if the user types in the char "y" then the program is repeated and if the user types in anything else then the program is ended.
I want to make it so that when the user types in only the char "n" the program ends and if the user types in anything apart from "y or n" then the program will ask the user to enter "y or N"
my code example:
//declare variables
char goOn = 'y';
while (goOn == 'Y'|goOn == 'y')
System.out.println ("Hello");

System.out.println("To re run the program enter Y. Or N to quit");
goOn = EasyIn.getChar();
//Write out end message
System.out.println("\n\nEnd of Program\n\n");

(ps.i am new to programming)
[ November 26, 2003: Message edited by: macca Mason ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Hi Macca
Welcome to the ranch. A couple of things, first and most importantly, you never asked a question. Does your code compile? Does it run? do you get any compilation errors? Or an any runtime exceptions? Or does it run fine and not behave as you expect it to?
The first thing that jumps out is your while statement:
while (goOn == 'Y'|goOn == 'y')
The | is not the one you're looking for. You most likely want the ||, for the logical or. The single | is the bit operator 'or'.
Also, in the future, when you post code use the UBB code tags to make it easier to read.

macca Mason

Joined: Nov 26, 2003
Posts: 13
Hi Dave.
Thx for replying.
Yeah my code runs fine, all I want it to do is ask the user at the end of the program if they wish to re-run the program or quit. By entering Y or N.
If the user enters anything apart from Y or N then I want the program to say "please enter Y or N". At the moment my program just quits when the user enters any character apart from Y. (when the user enters Y however the programs re-runs).
How can I make it so just Y and N are accepted characters in my while statement.

My full code is.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Think about a little loop that says

and a bigger loop that says

Can you see how to fit those together?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
you can put another while block after
goOn = EasyIn.getChar(); to evaluate user input
macca Mason

Joined: Nov 26, 2003
Posts: 13
Thanks Chi and Stan for the help.
I will try to put this in my code. (but finding where about to put it will be difficult)
Thanks alot. If I have any more questions I'll post again.
I agree. Here's the link:
subject: Loops
It's not a secret anymore!