String Integer Input check

P

Paul Wagener

Hi folks,

Problem:
User is expected to input a String type.
How to check if the input is valid and not of Integer type?

Thanks a lot!
Paul
 
D

Dave Neary

Hi,

Problem:
User is expected to input a String type.
How to check if the input is valid and not of Integer type?

Every string which can be parsed to an integer is a valid
String...

To check whether you can parse a string as an integer, try to
parse it

int i;
String s = "132";
try {
i = Integer.parseInt(s);
} catch (NumberFormatException nfe) {
System.err.println("s is not parseable as an integer");
}

Cheers,
Dave.
 
V

VisionSet

Paul Wagener said:
Hi folks,

Problem:
User is expected to input a String type.
How to check if the input is valid and not of Integer type?

Integer as text is a subset of String, that aside...

You probably don't want to throw exceptions if you expect it to be a String.
So do myString.toCharArray(), then step through and check each char with
Character.isDigit(myChar)
 
M

Michael Borgwardt

VisionSet said:
You probably don't want to throw exceptions if you expect it to be a String.
So do myString.toCharArray(), then step through and check each char with
Character.isDigit(myChar)


Note that Character.isDigit() returns true for quite a lot of digit characters
used in other languages.
 
E

Eric Sosman

VisionSet said:
Hi folks,

Problem:
User is expected to input a String type.
How to check if the input is valid and not of Integer type?


Integer as text is a subset of String, that aside...

You probably don't want to throw exceptions if you expect it to be a String.
So do myString.toCharArray(), then step through and check each char with
Character.isDigit(myChar)


That might be useful as a pre-screening step, but
is certainly not definitive. For example, what would
the scan conclude about "999999999999999999999999999"?
Or about "-42", for that matter?
 
V

VisionSet

So do myString.toCharArray(), then step through and check each char with
Character.isDigit(myChar)


That might be useful as a pre-screening step, but
is certainly not definitive. For example, what would
the scan conclude about "999999999999999999999999999"?
Or about "-42", for that matter?


The problem space was far from clearly defined, my solution was equal in
this regard the OP can glean from it what he will.
 
A

Ann

Paul Wagener said:
Hi folks,

Problem:
User is expected to input a String type.
How to check if the input is valid and not of Integer type?

Thanks a lot!
Paul
-------------------
If there is a '-', it must be first.
All other chars must be digits.
-------------------
public class TryInt {
public static void main(String[] args) {
String numStr = "-7898787";
int numInt = 0;
int start = 0;
boolean flag = false;
System.out.println("Input: \"" + numStr);

if (numStr.charAt(0) == '-') {
flag = true;
start = 1;
}

for (int i = start; i < numStr.length(); i++) {
char a = numStr.charAt(i);
if (Character.isDigit(a)) {
numInt = numInt * 10 + a - '0';
}
else {
System.err.println("Invalid integer character \'"
+ a + "\' at position " + i);
System.exit(1);
}
}
if (flag) numInt = -numInt;
System.out.println("Output: \"" + numInt);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top