its not possible to enter anything without using the enter key
22 Replies - 3725 Views - Last Post: 14 July 2010 - 07:06 PM
#16
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 11:15 AM
#17
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 11:34 AM
NoobKnight, on 14 July 2010 - 02:13 PM, said:
mostyfriedman, on 14 July 2010 - 12:04 PM, said:
no, i wasn't thinking about C. Anyway Dogstopper already confirmed that its working.
But, the OP wants to enter one character without using the "enter" key, not a string as Dogstopper tried.
input: jay
output: j
Take a look at the InputStream class. As System.in is an InputStream, you can read() one character in at a time as an int (or a byte if you pass a byte[] to read()). Then just cast to char.
Still gotta use the enter key, though.
#18
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 04:52 PM
NoobKnight, on 14 July 2010 - 01:13 PM, said:
But, the OP wants to enter one character without using the "enter" key, not a string as Dogstopper tried.
input: jay
output: j
input: jay
output: j
Read again.
Quote
Hello
I have a problem, I am trying to prompt the user to enter a number so I have designed my function to continue to loop (& ask again for a number) if it receives invalid input.
So when my function prompts the user to enter a number & then the user enters a character, my function should output "Invalid input" then ask the user again for a number...
But what actually happens with my function when the user enters a character is that my program goes into an infinite loop.
Why does this happen & how can I fix my code to just output "Invalid input" then ask again for the user to input a number.
I have a problem, I am trying to prompt the user to enter a number so I have designed my function to continue to loop (& ask again for a number) if it receives invalid input.
So when my function prompts the user to enter a number & then the user enters a character, my function should output "Invalid input" then ask the user again for a number...
But what actually happens with my function when the user enters a character is that my program goes into an infinite loop.
Why does this happen & how can I fix my code to just output "Invalid input" then ask again for the user to input a number.
It says absolutely NOWHERE the little WITHOUT ENTER KEY part...
#19
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 05:57 PM
Here it is Dogstopper, its in the middle of the thread.
gretty, on 12 July 2010 - 09:49 PM, said:
Thanks that worked 
one other question how do I take user input that is just a char (not a string but just a char)?
I know if I want the user to input a string I use: in.next();
I know if I want the user to input a int I use: in.nextInt();
I know if I want the user to input a float I use: in.nextFloat();
but what do I use if I just want to take in one character?
one other question how do I take user input that is just a char (not a string but just a char)?
I know if I want the user to input a string I use: in.next();
I know if I want the user to input a int I use: in.nextInt();
I know if I want the user to input a float I use: in.nextFloat();
but what do I use if I just want to take in one character?
#20
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 06:00 PM
I still don't see where it says "without hitting enter." I think the OP is looking for the equivelant of nextChar().
#21
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 06:01 PM
Dogstopper, on 14 July 2010 - 10:31 AM, said:
pbl, on 13 July 2010 - 05:15 PM, said:
mostyfriedman, on 12 July 2010 - 10:41 PM, said:
@OP: there's no method in Java that would just obtain a char from the user.. you could try this actually
char c = (char)System.in.read()
I am afraid that can't work... we would see it's use a lot more often... InputStream.read() is an abstract method that should be overload by the classes that extend InputStream
pbl, it actually does work...Give it a shot!
char c = (char)System.in.read();
System.out.println(c);
Quote
input: jay
output: j
output: j
OK my bad
#22
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 06:02 PM
I think this thread is turning into a ridiculous argument, the OP got plenty of help and tips already, lets just leave it there.
#23
Re: Infinite loop when unexpected variable type is input
Posted 14 July 2010 - 07:06 PM
You could on a GUI but not on a console application. Java was not really designed for console application... took JRE 5 to get the Scanner class

New Topic/Question
Reply




MultiQuote







|