quick stupid java question

NeoMadHatter

Platinum Member
Nov 29, 2000
2,355
0
0
johntwang.com
how can i just read input from the first command line input?

java Program 2 4 5 inputfile > out

i want to be able to get the following:

x = 2
y = 4
z = 5

and then read the rest of the input from the inputfile
and then redirect the output of the program to the file "out" as it shows i can.
 

onelin

Senior member
Dec 11, 2001
874
0
0
I'm rusty on this, but it seems to me like your 2 4 5 should be in the args array with that usage. Grab your 2,4,5 from there and assign the values to your x, y, and z?

I think you really want:
java Program 2 4 5 < inputfile > outputfile
What you do in the program is irrelevant as long as you print out to the screen what you want, with > outputfile it will go to the outputfile instead of the screen, and with < the input will be taken from your inputfile. I'm too rusty on my console Java IO to help you out w/ the actual commands though.

HTH
 

Mitzi

Diamond Member
Aug 22, 2001
3,775
1
76
The standard Java main() method takes one parameter, a String array which contains all arguments which were supplied on the command line. By looking at the length of the String array you will know how many arguments the user passed your application, you can then access each of these arguments as required and process as required.

Example...

public static void main(String[] arguments) {
// get the total number of arugments passed
int n = arguments.length();

//get the nth argument (substitute n with the parameter you want)
String myString = arguments[n];

}

If this isn't clear do a seach in Google for a begineer Java tutorial.

Cheers.
 

Garet Jax

Diamond Member
Feb 21, 2000
6,369
0
71
Remember that the array is 0 based and that it does not contain the name of the program.