Results 1 to 6 of 6
- 02-12-2013, 12:17 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
- 02-12-2013, 01:28 AM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 124
- Rep Power
- 0
Re: Is there a way to make a user only input an int?
Hello Harley,
this is very much possible to do. If you specifically want to check if it's an integer I'd suggest that you look into parsing the input and then have a look at the class NumberFormatException.
- 02-12-2013, 01:37 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Is there a way to make a user only input an int?
The only way to make a user enter an int is to apply a screwdriver to their keyboard.
if they enter a character which isn't an int i want an error message to appear
Assuming we are talking about a console based program...
* Read the input the user enters. (Eg as a character stream, Console, or Scanner. Pick one, or try all three.)
* Convert the input to an int. Scanner has a method that does this for you. Otherwise you convert the String to an int. Oracle's tutorial suggests one way, but I'd go for using the parseInt() method of the Integer class.
* Catch any exception that will occur if what they enters does not convert (ie is not an int).
I hope I haven't confused matters by pointing out so many alternatives. A common approach, these days, seems to be to use a Scanner and its nextInt() method.
- 02-12-2013, 10:08 AM #4
Member
- Join Date
- Dec 2012
- Location
- India
- Posts
- 6
- Rep Power
- 0
Re: Is there a way to make a user only input an int?
for your problem,we can handle through java script also
like below e.g
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
The function below checks if a field has been left empty. If the field is blank, an alert box alerts a message, the function returns false, and the form will not be submitted:
now you can try fo your case too.
i think this will help for solve your problem.
regards,
Reddi
- 02-12-2013, 10:35 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 02-12-2013, 11:13 AM #6
Member
- Join Date
- Feb 2013
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
How to make user input be of the type double?
By abv in forum New To JavaReplies: 9Last Post: 08-06-2012, 10:24 AM -
User input
By the ole buc in forum New To JavaReplies: 16Last Post: 12-11-2011, 08:08 PM -
how to make the check button get user input and display it
By ashraf in forum AWT / SwingReplies: 2Last Post: 09-27-2011, 07:46 PM -
how to get input from User
By Alvaro in forum New To JavaReplies: 7Last Post: 01-16-2010, 12:02 AM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 05:47 AM
Bookmarks