Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

user input when calling class

Status
Not open for further replies.

richATrichardjones

Technical User
Sep 22, 2002
24
GB
Hi

I have a class called test. When I call test with "java test" I want to give it parameters. For example "javac test.java" to compile it then "java test(2,20)"

I am not sure how to do this. The code so far is

class test{
int one;
int two;

public test(int IPone, int IPtwo)
{
one = IPone; //assing input num to the int one and two
two = IPtwo;
}

public static void main(String[] args){

//make object and pass it parameters
test mytest = new test(IPone, IPtwo);

//show parameters have been assigned (line for debugging)
System.out.println(one);
System.out.println(two);

}
}

This however does not work. I have done it before where I can pass it parameters within some other code. However I want to pass it parameters when I call it when I start the prog like so "java test(20,4)"

Thanks for any help

Richard
 
Hi Richard,
Basically what you want is to pass parameters into
Code:
main (String [] args)
. I can see from your test code you know how to do it with other methods since you are passing parameters to the
Code:
Test
constructor.
As you can see from the method signature of
Code:
main (String [] args)
, it takes a
Code:
String [ignore][][/ignore]
. Java takes anything typed after
Code:
java className
and places it in this array. Note that with more than one item you must place a space between each item.
Your example from above would look like:
Code:
  java Test 20 4
Hope this Helps,
MarsChelios
 
Thanks for the reply. I think my main prob was that I am too used to c++ type stuff where you pass it in brackets. Thanks again for the help. I was quite stuck and you have saved me a lot of time fiddling about

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top