next up previous contents
Next: Mathematical library Up: 2 Input, mathematical functions Previous: 2 Input, mathematical functions   Contents

Keyboard input

It would obviously be much more useful to create a program which could add together any two numbers rather than just two specific numbers given in the program. Recompiling every time you wish to try a program with new values is not satisfactory. The way to enter numbers via the keyboard into your program while it is running is therefore given below. Input is slightly more complicated than output, so you are not expected to understand the code at this stage.

The beginning of your program will require a few extra pieces of code, so that it looks like this:

/**
 * Purpose:  Shows things you must add to allow input from the keyboard.
 *           These include "import java.io.*" and "throws IOException" in 
 *           addition to the lines indicated in the main method.
 */

import java.io.*;

public class InputExample
{
    public static void main(String[] argv) throws IOException
    {
    // code needed for keyboard input
    BufferedReader br = new BufferedReader(
                          new InputStreamReader(System.in));
    String temp;

    // rest of your program code here, as usual

    }
}
Then whenever you wish to read something from the keyboard, simply write
    temp = br.readLine();
You will probably find it helpful though to add a line like System.out.println("Enter Input") or similar whenever you use this so that it is obvious the program is waiting for some input. When running the program, you will know to type in something when you see this prompt. Press the return key to finish and let the program continue with your input.

The input is stored in the string temp so if you want to use the input as anything other than a string it needs to be converted into the right variable type. Java provides a way to do this as shown in the examples below.7

x = Double.parseDouble(temp);             // string temp converted to double x
y = Float.parseFloat(temp);               // string temp converted to float y
i = Long.parseLong(temp);                 // string temp converted to long i
j = Integer.parseInt(temp);               // string temp converted to int j
k = Short.parseShort(temp);               // string temp converted to short k
m = Byte.parseByte(temp);                 // string temp converted to byte m
a = Boolean.valueOf(temp).booleanValue(); // string temp converted to boolean a

Note that x, y, etc. must first been declared as their respective variable types before you assign the result of the string conversion. There will be a chance to practice this input method in the next exercise.

This is the standard way to input data from the keyboard. It looks quite complicated, but you should just copy the relevant code whenever you want to input data from the keyboard and for the moment trust that it works. By way of a brief explanation: import tells the compiler that you will be using some software from outside your program. The name java.io.* tells it what this is and implies to the compiler where the necessary files can be found. If there is the possibility of an error occuring in the program, it can be handled using a Java feature called exception handling. The BufferedReader and InputStreamReader used to get the keyboard input can throw up exceptions or errors, in this case called an IOException. This must be handled in your program, and the simplest way to do this is to declare that main throws IOException. Later in the course you will come to understand these concepts better.

There will be a chance to practice this input method in the next exercise.


next up previous contents
Next: Mathematical library Up: 2 Input, mathematical functions Previous: 2 Input, mathematical functions   Contents
RHUL Dept. of Physics