Programming 101 – Lesson Two: Some basic IO and if statements
Almost any program that is at all advanced takes some sort of input, whether it be a file, or a command from the user. In this lesson we will be learning how to do some basic input and output (generally called IO) as well as using if statements in order to determine if the input is what we want in order to make a more advanced programs. If you continue with this series, IO will be quite important while working with bukkit plugins, as they have multiple parts to their IO. In a bukkit plugin you have commands, events being triggered, and configuration files for example. So in this project we will be asking the user to give us a value, then we will tell them if that value is even or odd.
Terminology:
- IO: Stands for input and output.
- Console: This could be either command prompt, the console within eclipse, or even the console program in OSX or Linux.
- Constructor: A constructor is what you use to make new instances (instantiate) of a class, you will be given an example later which will make it much more obvious I hope.
- Block: A block (more specifically a block of code) is a section of code which is blocked off with curly brackets (“{” and “}”). This block could be either an if statement, a loop, a method, or even a class.
- Modulus: More commonly referred to mod, this will tell us the remainder. For example 5 mod 6 = 1, and 5 mod 12 is 2.
- Primitive: There are 8 primitive data types in java (each of which has an object counterpart). For now you don’t need to know the difference between a primitive and an object, however it will be useful to know what the primitive data types are:
- byte: A byte represents an 8 bit signed integer (in 2′s compliment form), its values range from -128 to 127.
- short: A short represents a 16 bit signed integer (in 2′s compliment form), its values range from -32,768 to 32,767.
- int: An int represents a 32 bit signed integer (in 2′s compliment form), its values range from -2,147,483,648 to 2,147,483,647
- long: A long represents a 64 bit signed integer (in 2′s compliment form), its values range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- float: A float represents a 32 bit floating point value (or decimal value), at this point don’t worry about the range as it is above the scope of this discussion.
- double: A double represents a 64 bit floating point value (or decimal value), at this point don’t worry about the range as it is above the scope of this discussion.
- boolean: A boolean represents a true or false value.
- char: A char represents a single 16 bit Unicode character.
- String: String is not a primitive, however considering the special support given to it by the language, many people tend to think of it as a primitive.
- BufferedReader : A buffered reader is one of the many reading classes in java. One of its advantages is it’s readLine method which allows you to get a whole line of text.
- InputStreamReader : An input stream reader allows us to use an input stream as a reader, which is quite useful when reading from the console.
Project:
In this project we will be writing a program that asks you to input a number and it will tell you if the number is even. The goals are that you will learn how to do some basic if statements, read input from the client, and learn the modulus operation (granted it isn’t needed very often, but it’s still useful). First things first, time to make a new project, I will be calling this project Even Or Odd, and the class will be called EvenOdd. I will not go over how to do this as you should remember from the last lesson, if you do not, feel free to go back and look . Once that is done you will have our empty base again where we will begin. First we should probably tell our user what we want them to enter, so write an output statement like you learned in the last lesson as the first line of the main method asking them to give us an integer and telling them that we will tell them if it is even or odd.
Next we will have to take in our input from the user, to do this we will use what is called a BufferedReader. As you read before a buffered reader allows you to read a full line of text, which is quite useful for this application because we are not specifying how long their integer is. However the way that we read from the console is an InputStream, so we can not instantiate a BufferedReader directly as the BufferedReader only knows how to use a Reader when making a new instance of itself. The way that the engineers at sun/oracle got around this is by making the InputStreamReader, which takes an InputStream as an argument and gives you an InputStreamReader which can be used in our case. This may seem complicated but it is actually quite simple. We first instantiate a new InputStream reader like this:
InputStreamReader ISR = new InputStreamReader(System.in);
This will give us a new instance of InputStreamReader stored in the variable ISR using System.in . In eclipse you may have a red squigly under the InputStreamReader, that is because it hasn’t been imported yet, just hold your mouse over it and select “Import InputStreamReader (java.io)”. System.in is the input stream from the console which is exactly what we want. The InputStreamReader(System.in) is the constructor we will be using to create this instance. Any time you have the word new in a statement in java, that means that you will be instantiating something using a constructor. Ok we are half way there, now we have to create an instance of a BufferedReader to use to do the reading, to do this is just like the previous example, I will let you try this on your own, and if you get stuck feel free to reveal the code below.
Hopefully you tried that on your own, and even if you weren’t successful, that’s ok, you are still learning. That is the way that you will instantiate classes in java and you will be doing that a-lot, so with practice you will get it. Now that we have our BufferedReader, we will be able to read the input given to us by the user, so let’s do that now. I will describe what you want your code to do, and you try to write it, like before I will put my code after. This code will read a line from our buffered reader, and save that line into a variable of type String we will call num.
Now we have the string that the user gave us, from there we have to convert it to in integer. The Integer class has a method called parseInt we can use to do this. It takes a String as an input and returns an int. Note that the Integer class is the data type for the primitive int, each primitive has one, and they will always start with a capital letter (just like all primitives start with a lower case letter). So this code I will give you and then explain because I expect a question about it.
int myInteger = Integer.parseInt(num);
this will either return an int if the string is a valid number or it will throw a NumberFormatException, so just like when reading the input from the user, we will add a throws declaration. The reason that we can do Integer.parseInt(num) without instantiating our own Integer is because that is a public static method. You may remember me talking about the different protection levels, and a public static method is available to anyone at any time, even without instantiating the class. Ok our small project is almost done, now we either have the number or the program has crashed because the user gave an invalid input, so lets determine if the number is even or odd. In math class you may have talked about the Modulus (mod) operator, if not that’s fine, I’ll give you a quick crash course here as it is quite a simple idea (with profound impact on the world today). Modulus division is essentially the division you learned in second or third grade, where 5 divided by 2 would be 2 remainder 1, however we only get the remainder. As an aside, this simple idea is what allows RSA encryption to be so strong (arguably the strongest encryption method currently available). However back to the lesson, Java has a built-in mod operator, in place of the division sign you use the percent symbol so 5%2=1 (5 mod 2 = 1). So anything mod 2 will tell you if the number is even or odd, which is exactly what we want.
Now we need to form if statements. If statements allow you to do one block of code when a certain condition is true, there is also an else statement which allows you to do a different block of code if the condition is false. An if statement is written like the example below.
if(condition == true){
//do this
}else{
//do this
}
Please note the double = sign, this is an equivalency operator. You can think of the = sign in java as an assignment operator and == as a test to see if two values are equal. There is also a != which is the not equals operator. In our case we will be using the == operator however we could use the != operator and do it opposite if we wanted to. I believe I have given you all the info you need to come up with this code, so I will hide it and if you want to check against it, go ahead.
If you were able to figure it out without looking, great job, you are getting the hang of it, now there’s only one more step, and that is to write an output statement telling us if the number is even or odd. For this output we will tell the user “is even” or “is odd”. To do this you will have to concatenate some the variable number we took in originally with ” is even” or ” is odd” which is quite simple to do in java. As well as working as the addition operator, the + symbol can also be used to combine multiple strings together, you can think of it as adding strings. I will give you the output for evens and you can write it for odd numbers.
System.out.println(num + " is even");
Once you add your code to the else statement for if the number is odd you are ready to test the program like you did with the hello world example. Try a number and see if it works, also try a letter and you can see it throw an exception which I will show you how to deal with next time. In the next lesson I will teach you about loops as well as the “try catch” block, expanding on this lesson. However in the meantime below are some pictures of my eclipse console output. Also if you would like to see all the code, take a look at the spoiler all the way at the bottom of the post.
Acknowledgements:
- Information on the primitive data types were gathered from Oracles documentation available here
- Oracle/sun for keeping such good documentation using javadoc’s



