• Post Reply Bookmark Topic Watch Topic
  • New Topic

Controlling User Input  RSS feed

 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way you can control user input?

For example, is it possible to only allow the user to enter digits?

Thanks in advance.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input in a command prompt? a GUI? a web page? a paper napkin?

You're going to have to be more specific to get a meaningful answer on a forum!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damien,
Enter via the command line? Via Swing? On a web page?

The traditional way is to have a do while lop that does the validation and make the user keep re-entering until it is acceptable input.
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not using a GUI, I'm just using the scanner import
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for everyone's edification; this appears to be a continuation of this thread.

Winston
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to only allow the user to enter digits?


With a scanner object? Sure. What do you need, and int, double, something else?
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
is it possible to only allow the user to enter digits?


With a scanner object? Sure. What do you need, and int, double, something else?


I only want to allow the user to enter anything from 0 to 100, no negative numbers, letters or any other characters.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so 27.45?
 
Damien O Sullivan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:so 27.45?


No, just whole numbers too.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in what way is that not a number? Or do you mean "digits only"? [edit - sorry - didn't see the "whole numbers" part]

what about "0000000012"?

My larger point is that in programming, specifics MATTER. You can't write a program without knowing exactly what you want it to do. You have to think through every possible scenario you can...and then you still probably missed some.

The simplest thing to do is to let them enter whatever they want, as a String. Then, your program has to look at it and decide if it is valid or not. Then you have to decide what do do - quit, print a message, give them another chance (and how many chances)...etc.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!