java

Associate
Joined
26 Jan 2006
Posts
1,502
Hello,

Assume this is my main method:

public class mycode { public static void main(String[] arguments) {
body; } }

Is there a way to find out how many arguments were passed on from the command line when called?

For example,

java mycode a b c d

4 arguments were passed.

thanks
 
ok arguments are working as intented and exceptions handled.

Now I need a way to tell if the argument is integer(can be converted into one), double(similarly as with integer), or simply a string(as it is by default).

Is there an isInteger() thing in java or something else?
 
I run into problems again :(

I used try { } catch { } but now lets say the argument is not an integer (1) and it is a double (1.2) I need to store it in a double variable. Java wont let me declare vars in the try catch construct.

thanks for your time.
 
Code:
public class myclass
{
    public static void main(String[] arguments)
    {
        try
        {
            int argument0 = Integer.parseInt(arguments[0]);
        }   
        catch (NumberFormatException e)
        {
            try
            {
                double argument0 = Float.parseFloat(arguments[0]);
            }
            catch (NumberFormatException f)
            {
                System.out.println("Invalid Argument!");
            }    
        }
        
        System.out.println(argument0);
    }
}

The compiler will complain that argument0 variable is not there.
 
I understand, but my intention is to end up with argument0 being a double or int type according to what kind of argument was passed.
 
ok this works now. Lets say i get 2 arguments and they end up in argument0 and argument1. How I can show the bigest of the two etc? I cant think of a way to get the variable into an if statement. This is what confused me in the first place. I need a way to extract the arguments from the arguments String[] and then manuipulate them as normal variables with switch/if statements.

Thanks!
 
Lagz said:
It is not possible to do this in Java, since it would prevent most of Java's compile time type checking and lead to all kinds of horrible run time errors.

Why would you want to do this anyway? Surely you can just make it a double regardless of whether it is an integer or not?!

Thanks for your info lagz. Well I can still solve my problem if there is a way to display a double which happens to be an integer in integer form. Example, 1.0 show as 1 while 1.1 show as 1.1.

I dont know how to do this so I thought I could try use seperate vars :(
 
Back
Top Bottom