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.

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.

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.

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.

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:

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

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.

9 thoughts on “Java Tutorials: Series 1 Errata”

  1. 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

  2. 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 ?

  3. 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.

  4. 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

  5. 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.

  6. 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

  7. Great vids! I’m taking all the VTM knowledge to Eclipse now.

    The link for the zip doesn’t work right now though!

Leave a Reply

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