Infinite loop when unexpected variable type is input

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3725 Views - Last Post: 14 July 2010 - 07:06 PM Rate Topic: -----

#16 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 11:15 AM

its not possible to enter anything without using the enter key
Was This Post Helpful? 1
  • +
  • -

#17 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 11:34 AM

View PostNoobKnight, on 14 July 2010 - 02:13 PM, said:

View Postmostyfriedman, 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.
Was This Post Helpful? 1
  • +
  • -

#18 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 04:52 PM

View PostNoobKnight, 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


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.


It says absolutely NOWHERE the little WITHOUT ENTER KEY part...
Was This Post Helpful? 0
  • +
  • -

#19 NoobKnight   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

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.

View Postgretty, 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?

Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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().
Was This Post Helpful? 0
  • +
  • -

#21 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 06:01 PM

View PostDogstopper, on 14 July 2010 - 10:31 AM, said:

View Postpbl, on 13 July 2010 - 05:15 PM, said:

View Postmostyfriedman, 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

OK my bad
Was This Post Helpful? 0
  • +
  • -

#22 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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.
Was This Post Helpful? 1
  • +
  • -

#23 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2