/    Sign up×
Community /Pin to ProfileBookmark

I pretty new to this programming thing and was wondering how to display something when a person inputs a letter when they should of inputed a number. eg.

Please input a number: j

Incorrect input // This displays when the letter above is inputed

Please input a number:

to post a comment
Java

2 Comments(s)

Copy linkTweet thisAlerts:
@BigDogDec 18.2005 — I assume you are asking this about Java, and NOT Javascript. They are VERY VERY different, see sticky at the top of this forum for more info.

To test to see if something is a number, you can try to convert it to a number and see if it throws an exception. Below is an example for an Integer.


[CODE]String some_string_that_is_a_number = "63764982";
Integer some_int;

try {
some_int = Integer.parseInt(some_string_that_is_a_number);
System.out.println("some_int is an integer that is equal to " + some_int);
}
catch(NumberFormatException e) {
System.out.println("some_int is NOT an integer!");
}
[/CODE]
Copy linkTweet thisAlerts:
@WaylanderDec 18.2005 — Yeah it is java, there is no cmd window to print to in javascript....

While for argument sake catching an exception for does validation work, it isnt considered to be the correct way of programming the logic that your after. For something like a date its generally considered to be ok as the alternative checking is failry involved but for standard checking like numbers, money, telephone numbers its much better to use regular expressions. Exceptions are only supposed to be strictly used to catch actual errors, incorrect user input is not considered to be an error.


[CODE]

...
import java.util.RegEx.*;
...

public Int getUsersInt()
{

boolean _isValid;
String _usersInput;
int _usersInt;

do
{

boolean _isValid = true;

//ask user for input

Pattern _pattern = Pattern.compile("^\d$");
Matcher _matcher = _pattern.matcher(usersInput);

if (_matcher.find())
{
try
{
_usersInt = Integer.parseInt(usersInput);
}
catch(NumberFormatException e)
{
System.out.println(e);
System.out.println("Invalid Number Please Try Again");
boolean _isValid = false;
}
}
else
{
System.out.println("Invalid Number Please Try Again");
boolean _isValid = false;
}
}
while(isValid);

return usersInt;
}

...
[/CODE]


Best just to use this as an example as i have written it straight into the reply window and cannot compile it to test if it works.
×

Success!

Help @JonnyB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...