How can I get information about the type of input ?
String[] array in main
only accepts strings. If someone enters a number into args
how can I find out if it is a number written in string variable ?
Example code would be very helpful. ( Sorry if I asked really stupid question )

- 42,708
- 7
- 104
- 126

- 523
- 1
- 5
- 11
5 Answers
args[]
takes in all the arguments supplied to the main method, so args[0]
although as a String, if you expected the user to enter an Integer you could cast this with...
Integer.parseInt(args[0]);
Consider the fact though it may through a java.lang.NumberFormatException
so you'd have to check that in case the user entered an invalid input. If you wanted to do something based on if this is truly a number, look at StringUtils.isNumeric which is in Apache Commons Lang, why reinvent the wheel?
The reason I would go for this rather than try{} catch()
is because exceptions should be used for exceptional circumstances.

- 19,577
- 28
- 108
- 128
-
No i mean, if user enters a String my program should use different method, than if users enter a number. – user3407967 Mar 26 '14 at 12:46
-
Added notes regarding StringUtils.isNumeric – David Mar 26 '14 at 12:49
The type of your argument is always just String
. If that string is "", "Foo" or "182" is at this point totally arbitrary.
These strings may, or may not be valid representations of a number. You need to convert the strings to your number type (e.g. Integer.parseInt
) for int
.

- 1,575
- 8
- 18
You can use this code. This code will first check whether the input entered is number or not. If it is number it will parse that to num else it will give you the default value.
public static void main(String args[]) {
char chararr[] = args[0].toCharArray();
int num = 0 ;
boolean isNumber = false;
for (int i = 0; i < chararr.length;i++) {
if (Character.isDigit(chararr[i])) {
isNumber = true;
}
else {
isNumber = false;
break;
}
}
System.out.println("str is number "+isNumber);
if (isNumber) {
num = Integer.parseInt(args[0]);
}
System.out.println("number is "+num);
}

- 54,589
- 14
- 104
- 138

- 321
- 4
- 17
Simplest way is to check using parseInt
.
public static void main(String[] args) throws InterruptedException
{
try
{
Integer.parseInt(args[0]);
}
catch(NumberFormatException e)
{
System.out.println("It is not a number.");
}
}

- 8,385
- 12
- 60
- 90
-
I'd argue this is bad practice because it's not an exceptional circumstance, StringUtils would be a more appropriate solution. They shouldn't be used for flow control http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control – David Mar 26 '14 at 13:49
You can't "know" of which type a String
value is. What you may try is to check if a String
can be converted into some other type. Even if it can, it does not mean, the String
value was meant to be of that type. Here a example:
int value = Integer.parseInt("4711");
This statement would result in an Integer
with value 4711
, which does not mean, the String
was meant to be a codified Integer
.

- 7,972
- 27
- 35