473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how can I validate if what a user types is an integer or not

my problem is to ensure that what the user types for the index of an array is really a digit and not a letter...please answer a.s.a.p. thx:D
Nov 1 '08 #1
5 2865
JosAH
11,448 Expert 8TB
Have a look at the 'static int parseInt(String s) throws NumberFormatException'
method in the Integer class. It throws an exception when the String doesn't
represent a valid int value.

kind regards,

Jos
Nov 1 '08 #2
In regard with your answer...how ill I know if the user typed a string value if it automatically converts it into an integer? Well the result I want to get is not an exception I just want to place the values in an if statement and if that statement states that they are not of equal data type...it will display a .setText message that I will indicate...
Nov 1 '08 #3
JosAH
11,448 Expert 8TB
Well the result I want to get is not an exception I just want to place the values in an if statement and if that statement states that they are not of equal data type...it will display a .setText message that I will indicate...
Of course you have to catch that mentioned exception and take the appropriate
action(s). The method does its part, you do the other part.

kind regards,

Jos
Nov 1 '08 #4
So I have to try and catch? Can u teach me the basics of that methods
Nov 1 '08 #5
JosAH
11,448 Expert 8TB
So I have to try and catch? Can u teach me the basics of that methods
Simply wrap that method call in a try ... catch block; if the 'try' part completes
succesfully your String represented a number; if the 'catch' clause is executed
your String didn't represent a valid int number; it's some sort of an if-else structure. Look:

Expand|Select|Wrap|Line Numbers
  1. String s= // your String value
  2. int value; // the result of the method
  3. try {
  4.    value= Integer.parse(s);
  5.    // here the String represented an int
  6. }
  7. catch (NumberFormatException nfe) {
  8.    // here the String didn't represent an int
  9. }
  10.  
kind regards,

Jos
Nov 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: me | last post by:
I want to validate fields so that users can't enter anything but a-z or A-Z in some fields. If a user types in anything other than a character, it ignores the keystroke. I have experimented with...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
1
by: mats | last post by:
Hell My problem is that I do not know how to validate input that I let the user type into my datagrid In one grid I let the user to type in a value into a template column - how do I make sure...
4
by: Wysiwyg | last post by:
I need to validate a form to ensure that all of the fields add up correctly. I can't do this while the user is entering data since validation needs to be done after the entry is completed. What's...
10
by: Mike Logan | last post by:
I am using the "contract first" design methodology. Contract First is design the WSDL first then design the server and client. However I must design my XSD/XML Schema before anything. I am...
6
by: =?Utf-8?B?ZGlhdG9tQG5ld3Nncm91cC5ub3NwYW0=?= | last post by:
Hello, I have a data entry windows form. One of the text boxes allows the user to enter a string. I need this text box to only allow users to type in a negative integer value (e.g. -1, -2,...
1
by: developing | last post by:
Hello, This time I want to filter the 'browse for file' API so that certain drives/folders cant be accessed. Not sure how to go about this...here is the module I am currently using to browse...
7
by: Amit | last post by:
Dear Friends I need to write a Java Script for a string payment_code which comes populated from a text field , should contain only 0-9,A-Z,a-z,Space ' ',Hyphen '-',Full stop '.',Comma ',',Plus...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.