Results 1 to 6 of 6
- 04-15-2011, 07:55 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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');
}
}
- 04-15-2011, 08:18 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
this
reads a single byte (not necessarily a full character when dealing with unicode)Java Code:input = (char)System.in.read();
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.
- 04-15-2011, 11:30 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Thanks for the reply!!
But I am asked to use only system.in.read():mad:. Can sumthing else be done it?:rolleyes:
- 04-15-2011, 11:36 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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).
- 04-17-2011, 07:30 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
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???
- 04-17-2011, 11:05 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Accessing a variable from a loop
By mhz041986 in forum New To JavaReplies: 4Last Post: 04-04-2011, 08:49 AM -
While Loop Problem with Java Assignment Program
By welsh_rocker in forum New To JavaReplies: 9Last Post: 01-12-2011, 01:55 PM -
Turn JTextArea Input into Variable
By corbokhan in forum New To JavaReplies: 6Last Post: 10-25-2010, 03:32 AM -
Variable not initialized after while loop
By ejs7597 in forum New To JavaReplies: 6Last Post: 02-28-2009, 05:00 AM -
Really need help with an assignment... counter control loop...
By maxpower1000sa in forum New To JavaReplies: 7Last Post: 02-21-2009, 10:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks