473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Reading arguments in Java

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
4 12849
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
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

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
Paul wrote:


Integer.valueOf(args[0])

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

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: M.E. | last post by:
I have explained at the linked below my problem. Basically it is a Java code .. It is a dropdown box. when you select an item it goes to another text area .. You can add items to that text area....
3
by: Jaques | last post by:
Hi, I'm looking for a tool that converts a jar into an executable program (or something like that). I guess there are a couple of options: 1) Convert the jar into a real executable that runs...
7
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
8
by: AndrewTK | last post by:
Hello, I'm trying to read data from a socket and I'm not seeing what I'm expecting.... it seems to skip the first line of data. I am new to Python and just trying to test what I can do with...
5
by: csharpula csharp | last post by:
Hello, I am reading a line of arguments from command line,here is an example of such line: convert -d 01/01/07 -n New File 3 -p C:\MyDocuments What is the best way to read it if for example...
1
by: artic | last post by:
Hi all.. I am currently doing a project on Card Reading using Java. I need one help, which package is best to read the card in java?
1
by: monkey0525 | last post by:
I'm writing a program that reads information from three seperate classes. Here is my code: public class Animal { protected int id; protected String type; protected double mass; ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.