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.

![Follow [wcl] on Twitter](http://twitter-badges.s3.amazonaws.com/t_logo-a.png)

January 24th, 2009 at 8:01 am
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”);
}
}
}
January 27th, 2009 at 2:55 pm
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:
January 28th, 2009 at 12:44 pm
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
February 9th, 2009 at 1:41 pm
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
February 11th, 2009 at 5:00 am
Great tutorials…… keep up the good work
February 12th, 2009 at 1:10 am
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.
February 16th, 2009 at 9:41 am
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
March 3rd, 2009 at 7:58 am
This does indeed ROCK! Thanks!
March 29th, 2009 at 10:50 pm
i m outta words.. you are simply the best!!!
April 14th, 2009 at 11:45 pm
Thanks. Please tell me where i can download the javadoc you are talking about in this video.
April 15th, 2009 at 1:06 pm
You can generate it yourself:
javadoc Keyboard.javaAt the command prompt.
Cheers
Michael
May 14th, 2009 at 4:12 am
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
July 2nd, 2009 at 8:41 pm
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)
July 2nd, 2009 at 10:23 pm
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
July 2nd, 2009 at 11:52 pm
THANK YOU!!!
I went crazy!
Hope it’s the last of my questions
You honestly ROCK!
Thanks again.
December 10th, 2009 at 9:55 am
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?
December 10th, 2009 at 10:15 am
Can you post your code?
February 13th, 2010 at 6:32 pm
You can import the Scanner from java.util.Scanner
February 14th, 2010 at 6:06 pm
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