2

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 )

Meno Hochschild
  • 38,305
  • 7
  • 88
  • 115
user3407967
  • 433
  • 1
  • 5
  • 10

5 Answers5

3

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.

david99world
  • 18,271
  • 28
  • 102
  • 127
2

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.

Norwæ
  • 1,495
  • 8
  • 18
2

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);
}
Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122
JManish
  • 261
  • 2
  • 16
0

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.");
    }
}
codingenious
  • 7,785
  • 7
  • 55
  • 86
  • 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 – david99world Mar 26 '14 at 13:49
0

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.

Harmlezz
  • 7,524
  • 23
  • 34