Results 1 to 6 of 6
  1. #1
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Question Assignment of blank value of input variable after the first loop iteration

    Hello!

    I am writing a java program, which should display a menu and do some little things as per the options selected.
    I want to iterate through the menu till the user presses an invalid input or takes the Quit option.

    The problem is once the loop goes good. On second iteration, it shows the menu but does not let the user enter his choice. Byitself, it assigns to blank(' ') and makes the loop terminate(because of invalid entry) after the first iteration.

    char input;
    do {
    System.out.println("Select one of the following :");
    System.out.println("[D]");
    System.out.println("[E]");
    System.out.println("[F]");
    System.out.println("[Q]");
    input = (char)System.in.read();

    if (input == 'd')
    {
    System.out.println("Thats D");
    }
    else if (input == 'e')
    {
    System.out.println("Thats E");
    }
    else if (input == 'f')
    {
    System.out.println("Thats F");
    }
    else if (input == 'q')
    {
    System.out.println("Quitting...");
    // System.exit(0);
    }
    else
    {
    System.out.println("Invalid Input");
    // System.exit(1);
    }
    Runtime r = Runtime.getRuntime();
    r.gc();
    }
    while (input == 'd' || input == 'e' || input == 'f');
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    this
    Java Code:
    input = (char)System.in.read();
    reads a single byte (not necessarily a full character when dealing with unicode)
    but, at the very least, it leaves behind the "newline" which is what the next call to it reads.
    Wrap System.in in a BufferedReader (which also solves the Unicode problem as long as System in is actually using the default system encoding) and use readLine, then call charAt(0) on that resulting string. rather than using read on a generic inputstream.

  3. #3
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks for the reply!!
    But I am asked to use only system.in.read():mad:. Can sumthing else be done it?:rolleyes:

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Then, after getting the char, continue calling read until you get to the newline character, of course.

    But what if they enter a capital letter as shown in the menues, rather than the lower-case letters you're checking for?

    And I believe you have either misunderstood something in believing that you are only allowed to use system.in.read(). And, if not, you may want to ask about the consequences of multi-byte character encodings. Then again, maybe you would get bonus points for handling them (I am not going to tell you how, but it is possible).

  5. #5
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default

    Thnaks...i implement ur frst hint..it wrks..:)

    now...i hav one another thing... whn user gives input from the keyboard, it shud be automatically accepted(without pressing Return key).. wht can i do for dat???

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Nothing. You can't read the input from the command line until the shell flushes it's output stream (the inputstream you're reading from) and it doesn't do that until the user hits enter.

Similar Threads

  1. Accessing a variable from a loop
    By mhz041986 in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 08:49 AM
  2. While Loop Problem with Java Assignment Program
    By welsh_rocker in forum New To Java
    Replies: 9
    Last Post: 01-12-2011, 01:55 PM
  3. Turn JTextArea Input into Variable
    By corbokhan in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 03:32 AM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  5. Replies: 7
    Last Post: 02-21-2009, 10:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •