Logout

Console Input & Java Types

For these early notes it makes sense to look at two semi-related topics.

Console Input:

By "console input", we mean information that is input from the keyboard into a program in a non-GUI (Graphical User Interface) way. This is the way we have been running our programs so far in Netbeans. So this is normally text that is simply typed in where the insertion point is in a running program. It's the old fashioned way of doing things, but is good at this point since we don't have to worry about all the details of GUI application; rather we can focus on the programming itself.

Here is about the simplest console based input program you could make; one which prompts the user for input, takes that information in, and then uses it some way.

 1 
 2 package consoleinput;
 3 
 4 import java.io.BufferedReader;
 5 import java.io.InputStreamReader;
6 7 public class SimpleConsoleInput { 8 9 public static void main(String[] args) { 10 System.out.println("Type something in:"); 11 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 String input = br.readLine(); 13 System.out.println("The thing that you just input was: " + input); 14 } 18 19 } 20 21


An Explanation of the Lines of a Simple Console Based Input Program

package consoleinput;

Above, you see one Java "class". Most often there is more than one class that makes up an application. By explicitly stating which package the class belongs to, there is a better chance of all classes being able to find one another when they make use of one another.

 


import java.io.BufferedReader;
import java.io.InputStreamReader;

All computers which can run Java applications have a package of classes called "java". And in this package there is a sub-package called "io", which stands for 'input and output'. With the above two lines, the BufferedReader and InputStreamReader classes of java.io are "imported". After typing our "breader" short-cut line in Netbeans, we have to click on the light-bulb, which adds those two "import" lines for us.

But note that we are actually not "importing" all the functions of the BufferedReader and InputStreamReader classes, rather by having the import lines up at the top, whenever we use either two of these classes, we need not write out java.io in front of them. Since in our example, both classes BufferedReader and InputStreamReader come from the java.io package, the full way of writing that long line would actually be as follows (if we did not have the import lines):

java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

Instead, because we have the import lines above, we we simply have to write (or in our case have Netbean automatically write, via our "breader" shortcut):

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

and the program will know first of all that BufferedReader and InputStreamReader actually do exist, and furthermore, where to find them (in this case, in the package "java", and sub-package of it called "io".



public class SimpleConsoleInput {
         //All the code of the class goes within the braces.
}


This is the line that declares our class. And note that all of the code of the class must lie within the curly braces.

 

 


public static void main(String[] args) {

}


This is the one method that all applications need in at least one of their classes: main. It is the method that starts the program, though it may call other methods - which we'll see shortly.

- "public" means that it can be accessed by other applications and/or classes, which it has to be since it is called by a double-click of a user, for example. We will see later that some methods are "private", meaning that they can only be called by other methods in the same class.

- "static" means that only one copy of this method can be running on a single computer at one time. A good analogy of non-static things would be when you are developing Games in Flash, and you don't have just one asteroid, you have many copies of a single asteroid movie clip.

- "void" means that this method is void of a return value. More on this idea later.

- (String[] args) More on this later too, but basically args is an array of information that can be sent to this method. "args" is the name of the array, [] declares that it is an array (an array is basically a bunch of things all of the same type, like a bunch or Strings, or a bunch of ints), and String is the type of the array.
For us in Netbeans, you set the values of the args array by right-clicking on the project coffee cup icon, going to properties, clicking on Run, and typing them in the "arguments" field, separated by spaces.


System.out.println("Type something in:");

"println()" is a method that is part of the "out" object which is exists in the "System" class. The function of println is to print output to the screen, and then do a carriage return which moves the cursor to a new line. So the above line of code will display "Type something in:" on it's own line. Note that what is to be printed goes inbetween double quotes.


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

This complex line sets it up so that we can receive keyboard input to our program. You really don't have to understand it at this point, but for what it's worth, here's a full explanation of what is going on here. Working from inside out, right to left:
- "System.in" is an object of the System class which keeps track of the stream of input being (in our case) typed in with the keyboard.
- That information is passed to an InputStreamReader object, which can return the (ASCII number) value of letters to our program. ('A', for example has a standard ASCII value of 65). But it can only deliver one ASCII value at a time.
- So the InputStreamReader object gives it's values one at a time to a BufferedReader object which stores all of those values in a "buffer", or storage area.
-"br" is just the name I chose to call our new BufferedReader object that we are making with this line.
From this point in the program on, we can use the BufferedReader object named "br" to do a variety of things with keyboard input.

String input = br.readLine();

Here we use the "readLine()" method of the BufferedReader class to read a line from the keyboard input; that is all the letters typed until the Enter key is pressed. Those letters are assigned to a "String" we chose to call "input". So from this point on in the program, "input" will keep that value of the information typed in by the user. (Remember that "String" is the data type of Java used for words.)


System.out.println("The thing that you just input was: " + input);

The only thing new to notice here is that since we are outputting more than one thing (a litteral String of words, and the variable we called "input"), we use a + sign between the two.

 

Java Types

The other thing I went briefly over in class, which is somewhat related to the above code is the concept of data types. More details to come later on, but basically different types of data are kept in different data types in the interest of not wasting computer memory. For example, a single character of the "char" data type certainly does not have to use up as much computer memory as a fractional number with lots of precision, for example 452.589238329.

The Java "types" we will use most often are the following (though there are a few others):

Type Example Description
char 'a' A single character
String "hello" A "string" of characters
int 67 A whole number
float 2.78 A real number
double 3.147289321782 A real number with great precision
boolean true Either true or false

Things to note:

The char example is in single quotes, and when we declare a char, single quotes is what we use. Whereas the String example is in double quote, and that's what we use for Strings. So, the following are correct declarations of a char and a String.

char c = 'z';
String s = "Abracadabra, sis, boom, ba!";

The type String is capitalized because it is actually a class, where the others are not classes, and so all begin with a lower case letter.

The real number type float is called a float because the decimal point can "float" without it actually taking up more space in the computer. 2.7834 is stored the same way as 278.34, exept the position of the decimal is recorded as being different.

A double is also a real number, but it is alloted twice as much computer memory (64 bits, versus 32), and so it is able to be much, much, much more precise; i.e., it has many more significant digits.

We can declare a boolean as "true" or "false, but we don't put those words in double quotes, which would treat them as Strings.

boolean b = false;
boolean c = true;

We often use booleans in conditional situations, which we'll get into shortly, but here's a simple example:

boolean raining = true;
if(raining){ System.out.println("Stay inside."); }else{ System.out.println("Go outside and enjoy the great outdoors!"); }

 

Parsing of Types

An important consideration when dealing with types is what needs to be done if one type has to be converted to another. A good example of this is when we take in number input from the keyboard and wish to use those numbers arithmetically. All keyboard input actually is just treated as letters by System.in. So we need to change, or "parse" the String input into int or float or double types. The way we do this is seen in various examples as follows:

int x = Integer.parseInt("9");

String s = "700";
int y = Integer.parseInt(s);

We could then do:

int sum = x + y:
System.out.println(sum);

And get 709, rather than 9700, which is what we'd get if we added together 9 and 700 treated as Strings.