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?
 
You can use instanceof or just use Integer.parseInt and catch the Numberformat exception.
 
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.
 
drak3 said:
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.

Yes it will (I think!?!)? Post your code so we actually know what your doing.
 
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.
 
It's because you've declared arguement0 in the try block, therefore it goes out of scope when it encounters a closing brace }.

Declare it before the first try statement.
 
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.
 
Code:
public class myclass
{
    public static void main(String[] arguments)
    {
        try
        {
            int argument0 = Integer.parseInt(arguments[0]);
            System.out.println(argument0);
        }   
        catch (NumberFormatException e)
        {
            try
            {
                double argument0 = Float.parseFloat(arguments[0]);
                System.out.println(argument0);
            }
            catch (NumberFormatException f)
            {
                System.out.println("Invalid Argument!");
            }    
        }
    }
}

Something like that then?
 
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!
 
drak3 said:
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.

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?!

Code:
public class myclass
{
    public static void main(String[] arguments)
    {
        try
        {
            double argument0 = Double.parseInt(arguments[0]);
        }   
        catch (NumberFormatException e)
        {                
            System.out.println("Invalid Argument!");
        }
    }
}
 
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 :(
 
drak3 said:
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!

Code:
public class myclass
{
    public static void main(String[] arguments)
    {
        try
        {
            double argument0 = Double.parseInt(arguments[0]); 
            double argument1 = Double.parseInt(arguments[1]);
            System.out.println("Max Value Is: "+Math.max(argument0,argument1));
        }   
        catch (NumberFormatException e)
        {                
            System.out.println("Invalid Argument!");
        }
    }
}

I suggest you refer to the API for such trivial questions. You wont learn anything trying to learn Java by asking how to do everything/ It is possible to work out how to do almost anything using google and the API, which is located here:
http://java.sun.com/j2se/1.5.0/docs/api/

e.g: For maximum of two numbers, look in the Math class.
 
drak3 said:
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.

There are plenty of hacky ways to format a double differently if it contains an integer value. One way would be:

Code:
//get the textual form of the value
String myValueStr = arg0.toString();
if(myValueStr.endsWith(".0"))
{
  //remove the ".0" bit from the string
  myValueStr = myValueStr.subString(0,myValueStr.length-3);
}
System.out.println(myValueStr);

I didn't test that - and you might need to change ".0" depending on what is actually printed when a double is storing an integer value, but hopefully you get the general idea.
 
Back
Top Bottom