Arrays and Command Line Arguments5:34 with Craig Dennis
We will wrap up exploring how to pass arguments into your Java application as the program starts. We will have a well deserved introduction to the basics of arrays and how to access their different elements.
It's hard to believe we've made it this far without actually directly talking 0:00 about arrays. 0:03 We've definitely seen them, and 0:04 in fact as we saw briefly strings are just an array of characters. 0:05 Arrays are basically ways to store multiple items in a single variable. 0:10 You can get access to the different elements in the array and 0:14 you can use other properties that help you loop through them. 0:16 You can plan to learn more about these and other data structures in upcoming courses. 0:19 I'm introducing this to you now however because it's the last part of the Java 0:24 boilerplate code that we have to go over. 0:28 So we want to pass in the puzzle phrase when we call our hangman program. 0:30 This is done by passing in an additional argument to the Java program, 0:34 here let me show you how to do it. 0:38 All right so let me show you how to make one of these arrays. 0:40 So we'll make a single variable so I'll do string, and then we're gonna do these 0:44 brackets, open bracket, close bracket and we'll call that fruits. 0:48 And an array literal starts with a curly brace on some languages it 0:52 uses the indexing thing there but Java we use the curly braces. 0:57 So just be careful there, banana and cherry. 1:02 And now we'll have this thing, this object. 1:09 It's a single variable but we can access it by different indices. 1:11 So arrays are zero index based, so the first one is zero. 1:16 So if I do fruits I'm gonna get back "apple". 1:20 Cool, and I can also have, there is a property on these called fruits.link that, 1:24 show me how long my array is. 1:29 So there's three entries there, now, for more on arrays, check the teacher's notes. 1:31 But what I want you to see right now is, look at how this is declared. 1:35 And see this is declaring an array of strings. 1:38 And if we look at our main method here, it's an array of strings. 1:41 So what we wanna do is we wanna make sure that somebody passes in a value. 1:45 So, let's do that, so we'll say, if args.length, right. 1:49 So we know that it's an array that has something if it's equal to == 0, 1:54 means they didn't pass something in, right. 1:58 And what we'll do is we'll warn them, 2:00 we'll say System.out.println("Usage: 2:02 java Hangman, and then we'll just format like this. 2:09 This is kinda standard command line CLI sort of things so we'll say system. 2:14 There's another thing we, 2:20 have out and in, we also have an error and this'll be a little bit louder. 2:21 So ("answer is required") is the error that we're gonna send. 2:26 And then, I don't know if you remember this or not, but 2:30 we can exit out of our program by saying system.exit(1). 2:32 And now none of the rest of this will matter. 2:34 So now we know that we have, a value in args, if we get here we know. 2:36 So we're gonna, instead of treehouse let's go ahead and 2:42 let's use args[ 0], cuz we know that there's at least one there. 2:47 So now if I try to start this program and I don't specify it, 2:52 I'm gonna go ahead and get out of here. 2:56 So let's go ahead and we'll say clear and 2:59 Hangman.java, and java Hangman and 3:04 I didn't specify anything there at the end. 3:09 So is gonna tell me that I made a mistake. 3:14 Oop, so there is it is so Usage: java Hangman is required answer is required. 3:18 Awesome, so we block it, so let's apply a parameter. 3:23 So I'm gonna use the up arrow and here we go. 3:30 And now we have to guess what you actually are 3:32 P-R-O-G- A-M-E. 3:38 Programmer, you did it, one more thing I wanted to show is, look how 3:47 the class here is declared as public, this hangman class is declared as public and 3:51 you've probably seen that in all the classes that we've done. 3:56 Now typically this is something that you wanna do. 3:59 If you refer back to the access level modifier table that I shared in the very 4:01 beginning of this course you'll see that classes actually fall under the same 4:04 access that fields and methods do. 4:08 So if we wanted a class to be used outside of it's package, right. 4:10 So right now it doesn't have any declaration or other classes here, right, 4:14 or prompter. 4:17 And our Java just says class, anything outside the package couldn't access it. 4:18 So we should really be professional and put public in here. 4:22 Now I did that because there's a little trick with 4:27 Jshell that lets you import the classes if you don't put public there. 4:30 So that's why we did that awesome, so now it's labeled properly. 4:33 You know how arrays work, you can play this. 4:37 Glad we finally got to go over arrays. 4:41 You'll end up using these a lot and we'll talk about those and 4:43 more handy ways to structure your data in upcoming courses. 4:46 You've learned a ton and I love that we're now at a place in our learnings where we 4:49 don't need to ignore things in the boilerplate code anymore. 4:53 Let's look at it one more time just to show you what a good grasp you have on it. 4:56 So again when we run the Java program we pass it a class name. 5:00 What that does is it looks for a public method named main in that class. 5:05 Because it is a class method not a instance method 5:10 it is marked with the keyword static. 5:13 The method doesn't return anything at all, so its return type is void. 5:15 It then takes any arguments it was passed, 5:19 you can pass multiple arguments with spaces. 5:21 It puts together an array of strings and 5:23 then passes that as an argument into that method which you can access through this 5:25 parameter here called args in your program. 5:30 Pretty cool right. 5:32
You need to sign up for Treehouse in order to download course files.Sign up