Part 5: Your First Program

Sep 1, 2017
Part 5: Your First Program
  • Crash Course to Java

    Part 5: Your First Program



    This part should particularly interest the more active learners among you. We're going to actually make something! To follow this part, you're going to need the latest JDK installed (Windows | Linux | Mac). You will also need an IDE setup. I recommend IntelliJ IDEA (Windows | Linux | Mac), but you may prefer Eclipse (Windows | Linux | Mac). Either one will do.

    Note: My instructions will be from IntelliJ, but if you do choose Eclipse, you should still be able to follow along.
    Note by The947thTokay: I added some spoilers for Eclipse users to make it a little easier.

    Section A: Hello, World!
    Whether you've programmed before or not, this is probably a phrase you've heard. "Hello, World!" is a generic program that pretty much every programming tutorial for any language will teach you how to make. It's basically just a program that prints out "Hello, World!' Let's get started.
    If you're using IntelliJ, when you open it up, after going through the first-start setup, you should see something similar to this:
    [​IMG]
    Yeah I know, I need to update. I'll do it later :p

    First, we want to create a new project. To do that, click "Create New Project". (In Eclipse, File > New > Java Project, see spoiler). Now, follow through the dialogue boxes, hitting next where necessary and giving the project the name "HelloWorld", like so:
    [​IMG]

    [​IMG]
    Now type a project name:
    [​IMG]
    Hit that finish button!
    Now, you will have something like this:
    [​IMG]

    What we need to do now is expand the project over on the left so we can see the src folder:
    [​IMG]
    and then right click on the src folder and go to New > Java Class, and click it.
    [​IMG]

    In the dialogue box that comes up, type <your package name (see section 1D)>.helloworld.HelloWorld and press enter. A class file named "HelloWorld" will open!
    [​IMG]

    [​IMG]
    Right click src, click New -> Class.
    [​IMG]
    Now, we nee to add a package, and give our class a name.
    You could check the box to automatically create a "public static void main(String[] args)" method, but to do it like the IntelliJ tutorial, uncheck the box.
    [​IMG]
    Now you're ready to create the main method!

    Now, the first thing we need to do is make the "main" method. The main method is kind of like a lever for the JVM to pull to start your code. It must be public static so that the JVM can directly reach in and call it without any object instantiation, and it must be a void. Additionally, it must take an array of Strings as an argument. This is so you can pass arguments to the jar as you start it up (if you've ever started a spigot/bukkit server, you probably know you can pass arguments like -Xms####M and -Xmx####M to control how much RAM the server takes up, etc). We won't be using the arguments for this section, but we might in future additions to the guide.

    Anyway, this is what the main method looks like:
    [​IMG]

    Now, we're going to use something you've seen a few times throughout the earlier parts. Printing to the console using System.out.println. (You could also use System.out.print. println does the same thing, but it skips to the next line for you.)
    [​IMG]

    Now, right click your HelloWorld class in the navigation pane on the left. You should see an option that says something like "Run HelloWorld.main()". Click that.
    [​IMG]
    To run your program you need to right click your project name, click "Run As" -> "Java Application".
    [​IMG]
    Now, a console will appear at the bottom of your screen.
    [​IMG]
    Now that you know how to run, there won't be any more pictures of how to do it.
    It should be possible to understand what's going on below now.

    A console should've popped up at the bottom if one wasn't already there before, and... if everything turned out, we now get this!
    [​IMG]
    Congratulations. You made your first program in Java. Keep this if you're going on to the next section, we're just going to build off of it. :)

    Section B: User Input
    Are you ready to make your first User Interface? :eek:

    No, unfortunately we won't be making animated 3d buttons with particle and sound effects today. We'll be doing something way more fun! Typing into a console. Are you excited yet? You should be! Let's get started.
    If you haven't already, open up the HelloWorld project we created in Section A. If you want to keep it for sentimental purposes, feel free to create a new project instead.

    Now, empty out the main method.
    [​IMG]

    We're going to be using a couple of concepts from earlier in this guide: While loops, and object instantiation! First, we need to give ourselves some way to access the user's input. There are a few different ways to do this, but this time we will be using a Scanner. Scanners make it really easy to get different types of input like numbers and strings. So, we need to make a new Scanner. Let's do it!
    [​IMG]
    Make sure to import Scanner!
    [​IMG]
    Hmm.. seems we've got an error. That would be because Scanner's constructors all take arguments. For the purposes of this guide, we will be using the constructor of Scanner that takes an InputStream as the only argument. The InputStream we will be using is System.in.
    [​IMG]

    Alright, now that we've got our Scanner, we can use it to interact with the user. The simplest way to do so would be to use System.out.print(ln) to prompt for input, like so:
    [​IMG]
    Now if we you run it, it will prompt for your name. Type it!
    [​IMG]

    Cool, right? But that's not all. You can do that multiple times.
    [​IMG]
    Result:
    [​IMG]

    But.. that's awfully linear. What if you want to be able to type whatever you want and get a different response? We can do that too, using a while loop! We will be looping on Scanner#hasNextLine, a method which returns true if there is more input to read. In our case, there won't be an explicit end to input, so this loop will never end without us deciding it should. One way to exit out of the loop can be to press Ctrl+D, which prints an end-of-file character, telling the scanner there is no more input available. However, a preferred method of exiting the program is to have an input option like "EXIT". We will be doing that.

    So, first we need the while loop, like so:
    [​IMG]
    Now, as we did previously, we just need to use scanner.nextLine() to read the next line of input. Let's get that as a string:
    [​IMG]

    Let's implement our "EXIT". This just takes a simple if statement.
    [​IMG]
    I added in that last line so it will print "Goodbye!' when the program is exiting.

    Simple, right? Now if you run the program, it will prompt you for input. If you then type "EXIT", the program will exit.
    [​IMG]

    But of course, that's not all we can do! Using some else statements (or a switch statement), we can make this program way more functional! Just play around with it.
    [​IMG] [​IMG]

    As you can see, while this isn't exactly a perfect form of input, this could be used to make some pretty interesting things!
  • Loading...
  • Loading...