Java Tutorial 04 – Control Structures

This video looks at control structures. Specifically, if/else blocks and switch blocks. By the end of it you will be able to write programs that make decisions on what to do.

UPDATE:I just realised I forgot to include Keyboard.java, which is required for this video. It is now uploaded.

[flashvideo filename=”video/01_04_java_vtm_control_structures.mp4″ width=”480″ height=”360″ /]

Note that this video is 800×600, so hit the full screen button to get more readable detail.

Published by

Michael Marner

My name is Michael, and this is my website. I am currently working as a Research Fellow at the University of South Australia's Wearable Computer Lab. I am also the chair of the board and an announcer at Adelaide based community radio station Three D Radio 93.7FM.

19 thoughts on “Java Tutorial 04 – Control Structures”

  1. Hi Michael

    Please please help. Going great guns up until VTM 4 Keyboard test and now its seriously gone Pete Tong. I have looked up the new “Scanner”class, and I have written out the code from your KeybpardTest notepad, but can I compile it, can I hell. I have tried writing out all of the info twice, looking up notes on Java help sites and I still cant get it to compile.

    Can you please try and send me a solution. I have copied put the code I have wrote in notepad++. Can you spot the problem;

    public class KeyboardTest {

    public static void main(String [] args) {

    int testInt;
    double testDouble;
    char testChar;
    string testString;

    Scanner scanner = new Scanner(System.in);

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    //Get the data from the keyboard
    System.out.println(“5”);
    testInt = scanner.nextInt();

    System.out.println(“123.456”);
    testDouble = scanner.nextDouble();

    System.out.println(“a”);

    //readline returns a String, so we get the character
    testChar = reader.readLine().charAt(0);

    System.out.println(“Please help”);
    testString = reader.readLine();

    //Print out the results
    System.out.println(“5” + testInt);
    System.out.println(“123.456” + testDouble);
    System.out.println(“a” + testChar);
    System.out.println(“Please help ” + testString);
    }

    catch (IOException e) {
    System.out.println(“Error getting data from keyboard”);
    }

    }

    }

  2. Hey Dean

    You’ve got a few issues. Firstly, to use Scanner and do IO you need to import some packages. Scanner lives in java.util and you should also import java.io like so:

    Next, you have to remember that in Java Strings are Strings, you had a lower case S. The convention is that all class types should start with a capital letter.

    Finally, you are missing the try statement. You must have a try block that preceeds a catch block.

    So, the final code looks like this:

  3. Wow, thank you, I’m 10 and I am very interested in learning Java. I have read many books, but your free video are the best.

    Thank you

  4. Hi Michael,

    Great tutorials so far. I really like them. Could you post the keyboard.java file so I can look over it and use it for some program ideas I’d like to try out. Thanks

  5. I just started learning Java using BlueJ with your tutorials (They’re awesome!).

    I created a new project with a class named Keyboard and put someone’s code I found on the internet in it.If I create a new class in that project, I can use the keyboard methods.
    But when I try to use it in another project I can’t use it.
    So is there anything I can do so that I can use for example “Keyboard.readInt()” anywhere just like the “System.out.println” command?

    Thanks.

  6. Hey.

    Unfortunately no. System is a built in class that is a core part of the Java SDK, Keyboard is simply a file I wrote to simplify things. You will have to have a copy of Keyboard.java, or at least the compiled class file in for each of your projects. The way around this is to not use my Keyboard class at all, and instead use the built in Scanner. See Series 1 Errata for a discussion on how you can use Scanner as a replacement for my Keyboard class.

    Cheers
    Michael

  7. Michael,
    I’m brand new to Java and having to come up to speed fast to write a plug-in for an existing software package. The plug-in will translate .csv files to an XML-like format, called MXML. Your tutorial is super! Thanks, from another drummer.
    Dana

  8. HELP!!!

    Hi Michael,

    Great tutorials. I’ve done great so far, until no.4.
    For some reason when i try to compile (c:\java>javac *.java) i get this:

    “Keyboard.java:19: class Keyboard is public, should be declared in a file named Keyboard.java
    public abstract class Keyboard {
    ^
    1 error”

    What went wrong?… i’ve looked at the file, and gone over everything more than twice, but didn’t know what to do.

    Please help me, i’m stuck for now.

    This is what i wrote in notepad++:

    public class KeyboardTest {

    public static void main(String[] args) {

    int testInt;
    double testDouble;
    char testChar;
    String testString;

    System.out.println(“Please enter a int:”);
    testInt = Keyboard.readInt();

    System.out.println(“Please enter a double:”);
    testDouble = Keyboard.readDouble();

    System.out.println(“Please enter a char:”);
    testChar = Keyboard.readChar();

    System.out.println(“Please enter a string:”);
    testString = Keyboard.readLine();

    System.out.println(“The int you entered was” + testInt);
    System.out.println(“The double you entered was” + testDouble);
    System.out.println(“The char you entered was” + testChar);
    System.out.println(“The string you entered was” + testString);

    }

    }

    (I used the “Keyboard.java” file you gave)

  9. Hey Johnny

    Make sure that the file is called Keyboard.java, with a capital K. I think when I uploaded the file wordpress converted the filename to all lower case. It must have a capital K for Keyboard.

    Hope that helps
    Michael

  10. THANK YOU!!!

    I went crazy!
    Hope it’s the last of my questions 🙂

    You honestly ROCK!

    Thanks again.

  11. Hi Michael,

    I am going along with your tutorials and I get to the part when you write out

    System.out.println(“Please enter an int”);

    I do it for all of them and it compiles fine but it does not print out the statements. Can you help?

  12. Yes

    If you have a look in the errata page, I’ve given some code that uses Scanner. Back in the pre Java 1.5 days, Scanner didn’t exist, and it was a bit of a pain to read from the keyboard.

    Cheers
    Michael

Leave a Reply

Your email address will not be published. Required fields are marked *