Download presentation
Presentation is loading. Please wait.
1
Command Line Arguments
On page 188
2
Using command line arguments
There is a way to get information from the command line (DOS prompt) into main() A command line argument is the information that directly follows the program name on the command line The arguments are stored as Strings in the String array passed to main (String args[]) To access the information, access the String array
3
Example A:\>java JavaTest GO BUFFALO BILLS
“GO”, “BUFFALO”, & “BILLS” are three command line arguments that…… go into an array of Strings where….. args[0] -> |G|O| args[1] -> |B|U|F|F|A|L|O| args[2] -> |B|I|L|L|S| This array of Strings is passed to main public static void main (String args[ ])
4
Another example A:\>java Test EDDIE DROPPED THE BALL
Are four arguments that go into an array…. args[0] = |E|D|D|I|E| args[1] = |D|R|O|P|P|E|D| args[2] = |T|H|E| args[3] = |B|A|L|L| public static void main (String args[ ])
5
continue Notice on the previous example that a space is the delimiter (separator) between each command line argument How many arguments are in each of the following examples?? A:\>java T01 bears eat oats and goats eat A:\>java T02 The BIG brown fox A:\>java T03 a b zecc c c c A:\>java T a bb ccc ddd5
6
args.length & Integer.parseInt()
String args[ ] defines an array of Strings remember, subscripts to arrays start with 0 args [0] is the first String args[1] is the second String……etc args.length is a method that returns an int that indicates how many elements are in the array int arrayLength = args.length; to convert a String to an int int example1 = Integer.parseInt(args[0]);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.