Java Tutorials: Series 1 Errata

Well, the Java VTM series 1 has been incredibly well recieved. Thankyou everyone for your support and comments about my videos. However, I’ve got a couple of things to fix regarding the videos, so here are some series 1 corrections.

So whats wrong with the videos? Nothing actually, but times change. Java has evolved over the years and the API gets bigger with every release. See, when I started learning Java, version 1.4 had just been released. In this version, like the version before that there was no simple way to extract data from the keyboard. Enter the Keyboard class. This is a java file that I wrote for the Java VTM. It has simple methods for getting data from the keyboard, so that you don’t have to do it yourself.

However, times change. With the release of Java 5 extracting data from the keyboard has never been easier. What this means for you is that you can pretty much throw my Keyboard class away. It is now obsolete. There is now a standard Java class called Scanner. Have a look in the Java API. There are all these methods like nextInt, nextDouble, nextLine etc. What Scanner does is provide a simple way to get data from the keyboard (any InputStream actually), so we don’t have to do the hard work ourselves.

Lets look at at the following code from Video 4 in the VTM. This is how the video explained how to get data from the keyboard.

int testInt;
double testDouble;
char testChar;
testInt = Keyboard.readInt();
testDouble = Keyboard.readDouble();
testChar =  Keyboard.readChar();

Now lets change the code so we use Scanner instead. The first thing we need to do is create an instance of Scanner. There are a couple of constructors available, but the one we are interested in takes an InputStream as its parameter. If we want to get data from the keyboard, we need to pass in System.in as the parameter.

Scanner keyboard = new Scanner(System.in); //create a scanner object

Now that we have the scanner ready to go, we can use all of its methods to access data that I did using my Keyboard class. The code below shows how we can get numbers from the keyboard.

testInt = keyboard.nextInt();
testDouble = keyboard.nextDouble();

So what about characters? Well, there isn’t a dedicated method for getting a character from Scanner. Same with Strings. There is a method called nextLine() in Scanner, but it doesn’t really do what you expect. If there is a line waiting to be read on the InputStream (such as in files), it will read the line. However, if there isn’t a line waiting to be read you will get an exception! Not fun. So, for doing character and String reading a BufferedReader is the way to go.

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

With BufferedReader you can read in lines or characters from an input stream. So to get a character and string you would do the following:

System.out.println("Please enter a char");
//readline returns a String, so we get the character
testChar = reader.readLine().charAt(0);
 
System.out.println("Please enter a String");
testString = reader.readLine();

Note that the code above should be contained within a try block, as an IOException may be thrown.

So with that, throw away Keyboard.java because you don’t need it all. The ever expanding Java API now has all of Keyboard’s functionality and a whole heap more. Finally there is an easy way to get data from the keyboard, without having to write your own classes. Here is the full Java file from VTM 4, using Scanner and BufferedReader instead of the Keyboard class.

Later
Michael

8 Responses to “Java Tutorials: Series 1 Errata”

  1. Chris (Cdoggy) Says:

    Hello, do you still plan for series 2? Also, do you have any knoledge of html, if so would you be interested in doing a tutorial about it?

  2. Michael Says:

    Hey Chris

    Yes, I do plan on making a series 2. It is finding time to plan the videos which is hard at the moment. Since the first series covers the more basic stuff, I only had to spend maybe 15 minutes planning out each video. To do a series with more advanced content requires a lot more planning up front. But yes, they will get done.

    As for HTML, yes I know it fairly well but am not that interested in making videos about it. I have considered making some PHP videos for web development, but considering how long it takes me to get a Java video done I don’t want to commit to anything.

    Cheers
    Michael

  3. Chris (Cdoggy) Says:

    Great thanks to hear from you, I’ll be seeing you around I guess, feel free to talk to me on MSN or X Fire.

    Thanks man = }

  4. Beginner-coder Says:

    Hello!
    Thank you, but I cant get it work!
    I’m not sure where I’m gonna post the codes and the zip doesn’t work :(
    Can you make the zip again ?

  5. Andrew (Coding Newb) Says:

    I just wanted to say thank you so much for this series of videos. I love learning new things and coding has become my new hobby. I started with the verry simple html, and then basic, and now java (thanks to you). Awesome vids, great teaching, and magnificent accent, lol.

    Thanks again for everything!

    Andrew M.

  6. New at programming Says:

    Hi,
    I just wanted to thank you for your effort, the videos are awesome and are without any doubt the best video tutorials i have seen. Best of luck with Series 2

  7. axel Says:

    thank you.
    thank you, Thank You, THANK YOU!

    I’m American, and I think the accent is fun! Keep up the good work. I’ve forwarded your site to a few friends that want to learn Java, and aren’t taking it from a school like I am. Hopefully they find it as well rounded as I do.

  8. Genady Says:

    Yes, I know you already saw a lot of times, BUT.
    I want to join all the good fellows here and thakn you
    it is actually a great peace of work you have done

    looking forward for the next VTM’s

Leave a Reply