simple loop question

Discussion in 'Java' started by matt, Jul 16, 2005.

  1. matt

    matt Guest

    Im trying to write a program that asks a user to type A, B, C, or Q.
    When the user types Q, the program ends. When the user types A, B, or
    C, the program displays the message "Good Job!" and then asks for
    another input. Its for a java course im taking and ive been at it for
    an hour and a half with no luck. Any help will be greatly appreciated.
    matt, Jul 16, 2005
    #1
    1. Advertising

  2. matt

    Alan Krueger Guest

    matt wrote:
    > Im trying to write a program that asks a user to type A, B, C, or Q.
    > When the user types Q, the program ends. When the user types A, B, or
    > C, the program displays the message "Good Job!" and then asks for
    > another input. Its for a java course im taking and ive been at it for
    > an hour and a half with no luck. Any help will be greatly appreciated.


    Show what you have so far and describe what's wrong. Otherwise, it
    looks like you're asking someone to completely write it for you.
    Alan Krueger, Jul 16, 2005
    #2
    1. Advertising

  3. matt

    matt Guest

    Well thats the thing, im really not sure how to go about doing this but
    here is what I started writing.

    public class ABCInput
    {
    public static void main(String[] arg)throws Exception
    {
    char response;
    do
    {
    response = (char)System.in.read();
    // if(response != 'A' || response != 'B' || response != 'C' ||
    response != 'Q')



    while(prompt == 'A' || prompt == 'B' || prompt == 'C')
    {
    System.out.println("Good Job");
    response = (char)System.in.read();

    I definitly dont want someone to write it for me, that would not
    benefit me in any way. Pointers in the right direction would be great
    though.
    matt, Jul 16, 2005
    #3
  4. "matt" <> writes:

    > response = (char)System.in.read();


    You need to realize that System.in is (normally) line-buffered, so
    nothing gets to your program until the user hits enter. Thhen you get
    what they have typed, inluding the line terminator character(s). You
    assume you only get one of the four characters.
    Tor Iver Wilhelmsen, Jul 16, 2005
    #4
  5. matt

    Roedy Green Guest

    Roedy Green, Jul 16, 2005
    #5
  6. matt

    matt Guest

    Figured it out, guess I just needed some rest. Thanks to everyone who
    replied.


    public class ABCInput
    {
    public static void main(String[] arg)throws Exception
    {
    char response;
    response = (char)System.in.read();
    System.in.read();

    while(response != 'A' && response != 'B' && response != 'C' &&
    response != 'Q')
    {
    System.out.println("Invalid response please choose again.");
    response = (char)System.in.read();
    System.in.read();

    while(response == 'A' || response == 'B' || response == 'C')
    {
    System.out.println("Good job! Please try again.");
    response = (char)System.in.read();
    System.in.read();
    while(response == 'Q')
    {
    break;
    }
    }
    }

    }
    }
    matt, Jul 16, 2005
    #6
  7. matt

    Wibble Guest

    matt wrote:
    > Figured it out, guess I just needed some rest. Thanks to everyone who
    > replied.
    >
    >
    > public class ABCInput
    > {
    > public static void main(String[] arg)throws Exception
    > {
    > char response;
    > response = (char)System.in.read();
    > System.in.read();
    >
    > while(response != 'A' && response != 'B' && response != 'C' &&
    > response != 'Q')
    > {
    > System.out.println("Invalid response please choose again.");
    > response = (char)System.in.read();
    > System.in.read();
    >
    > while(response == 'A' || response == 'B' || response == 'C')
    > {
    > System.out.println("Good job! Please try again.");
    > response = (char)System.in.read();
    > System.in.read();
    > while(response == 'Q')
    > {
    > break;
    > }
    > }
    > }
    >
    > }
    > }
    >

    At least your not comparing prompt instead of response. The nested
    while loops are a little wierd.
    Wibble, Jul 16, 2005
    #7
  8. matt

    Roedy Green Guest

    On 16 Jul 2005 05:35:46 -0700, "matt" <> wrote or
    quoted :

    > while(response != 'A' && response != 'B' && response != 'C' &&
    >response != 'Q')
    > {
    > System.out.println("Invalid response please choose again.");
    > response = (char)System.in.read();
    > System.in.read();
    >
    > while(response == 'A' || response == 'B' || response == 'C')
    > {
    > System.out.println("Good job! Please try again.");
    > response = (char)System.in.read();
    > System.in.read();
    > while(response == 'Q')



    This sort of logic is often nicely handled with switch. OO purists
    disdain the switch, but the code seems easier to maintain to me.

    loop: while ( ... )
    {
    switch ( response )
    {
    case 'A':
    case 'B':
    case 'C':
    reward...
    break;

    case 'Q':
    break loop;

    default:
    complain...
    break;
    } // end switch
    } // end while


    note how you repeated the logic "response = (char)System.in.read();"
    This is a symptom there is as simpler way to write the code where that
    appears only once.

    You are writing this as if were c. System.in.read is not getc. In
    Java, you have to read a LINE at time, groan, i.e. read a string and
    pick out the first char of the line. Best to put that logic in its
    own method, a kludged getC that reads the first char off a line,
    converts to upper case, and returns 0 if there is no char. You also
    need to deal with EOF. See http://mindprod.com/applets/fileio.html
    to get it to generate you sample code to deal with the console.


    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jul 23, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,381
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,230
    Tim Slattery
    Mar 30, 2005
  3. takayuki
    Replies:
    2
    Views:
    278
    Calvin Spealman
    Jun 16, 2008
  4. takayuki
    Replies:
    17
    Views:
    471
    John Salerno
    Jun 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page