By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Reading arguments in Java

P: n/a
Hi,

I have a question.

Let's say I want to make a simple java program where the first argument
passed to the java program is doubled and returned. I.e the reult of doing:
java Double 5 - would return 10.

This is turning out to be really hard as I have no idea how to "access" this
argument in java. If someone could help me out that would be really cool.

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Munk wrote:
Hi,

I have a question.

Let's say I want to make a simple java program where the first argument
passed to the java program is doubled and returned. I.e the reult of doing:
java Double 5 - would return 10.

This is turning out to be really hard as I have no idea how to "access" this
argument in java. If someone could help me out that would be really cool.

Thanks


args[0] in the main method.

To change it to an Integer:

Integer.valueOf(args[0])
Jul 17 '05 #2

P: n/a
Munk wrote:
Let's say I want to make a simple java program where the first argument
passed to the java program is doubled and returned. I.e the reult of
doing: java Double 5 - would return 10.

This is turning out to be really hard as I have no idea how to "access"
this argument in java. If someone could help me out that would be really
cool.


The main method's argument contains the command line parameters to your
program.

Example:

public class Hello
{
public static void main(String args[]) {
if (args.length == 1) {
System.out.println("Hello " + args[0]);
} else {
System.out.println("Hello World!");
}
}
}

--
Jonas Kongslund
Jul 17 '05 #3

P: n/a

If you're using applets you can use the getParameter function. For example,
I start appletX with these paremeters

<html code>
<APPLET codebase=../.. code="appletX.class" width=543 height=297>
<PARAM NAME = data VALUE = test >
</APPLET>

Now you can get that 'data' parameter by calling getParameter( "data" )
somewhere. Maybe this works with normal Java programs as well.
But if I'm right you can't name the argments so I doubt if you can
use it then. In that case, indeed use the main method of your app, like
Jonas and Paul showed.

Greetings,
Rick
Jul 17 '05 #4

P: n/a
Paul wrote:


Integer.valueOf(args[0])

or Integer.parseInt(args[0]) returns a primitive

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.