You are on page 1of 9

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATION


Chapter Objectives:
Upon completion of this chapter, you would have learnt: Simple Command to Input Character Simple Command to Input String Simple Output Commands How to Convert String to Integer How to Convert String to Real

4-1

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

4.1 Simple Input Command


A program that accepts values at run time is interactive because it exchanges communications, or interacts with user. In order to provide values during the execution of a program requires input, and the form of input that is used is keyboard entry from the user. Here, we shall be looking at some standard input commands that are used in Java programming language.

4.2 Character Input Command


In Java programming language, the characters are read from the keyboard by calling the System.in.read() method. This is one of Javas console functions. This method accepts input from the keyboard. The in object has access to the method named read() that retrieves data from the keyboard. The example below shows a program that accepts simple user input.

public class CharacterInput { public static void main(String[] args) throws Exception { char charInput; System.out.print(Enter a character : ); charInput=(char)System.in.read(); System.in.read();System.in.read(); // absorbs the Enter key.

System.out.println(\nThe character entered is +charInput); } } In the above example, the string Enter a character prints on the screen. A message requesting user input is commonly known as a prompt because it prompts or coaches the user to enter an appropriate response. The System.in.read() is used to obtain a character

4-2

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

input entered by the user from the keyboard. The read() method accepts the returned integer into a character. The cast(char) converts the returned integer into a character. By default, the standard input is line buffered, so the Enter key must be pressed before any characters that we type will be sent to the our program.

There is, however, a problem when we use read() to accept a character from the keyboard, every key we press including the Enter key is accepted, one at a time. Thus, we can absorb the Enter key after each input by reading in with two read() method calls.

Since we are using the System.in.read() method, in the main() method header, a phrase throws Exception must be added at the end of the line. It is necessary to handle input errors. An exception is an error situation. Because errors should be infrequent, and all sorts of errors can arise, thus, we can let the compiler handle the problem by throwing the exception, or passing the error to the operating system. By placing throws Exception after the main() header accomplishes this and a program, which reads keyboard input will not be able to compile successfully without this phrase.

The output of the above program is shown below:

4-3

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

4.3 String Input Command


In Java programming language, in order to allow a user to enter a string from the keyboard, the InputStreamReader() is required to read characters from the standard input. It converts the raw bytes into Unicode characters. Besides, the BufferedReader() is also used for it provides the readLine() method which allows the program to read from standard input a line at a time. import java.io.*; public class StringInput { public static void main(String[] args) throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in )); String studentName;

System.out.print(Enter student name:); studentName = input.readLine();

System.out.println(Your name is + studentName); } }

The example above shows the use of the readLine() method to allow the user to enter a string. Java implements I/O operations through streams. The streams are represented by two abstract classes, which include the InputStream and OutputStream. These classes are found in the java.io package. Thus, at the beginning of the program, the java.io package is imported into the program. To import an entire package of classes an asterisk(*) is used. It acts as wildcard symbol to represent all the classes in a package.

4-4

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

The output of the above program is shown below:

4.4 Simple Output Command


In Java programming language, the terminal window is used as the standard output. System.out is a PrintStream object that allows us to write to standard output. There are two methods used in Java programming that allows the user to display the input that had been entered to the screen, which include the print() and println() method. The difference between the two methods was mentioned in the earlier chapters. By using the println() method, after a statement is displayed, the cursor will be position on the next line. If the print() method is used, the cursor will be position on the next available position but on the same line.

4.5 Converting String to Integer


If a String contains all numbers, we can convert it from String to a number so that we can use it for arithmetic calculation, or to use it like any other number. To convert a String to an integer, we can use the Integer class, which is part of the java.lang package, and is imported into programs we write automatically. A method of the Integer class, which is parseInt(), is used to convert String to integer. It takes a String argument and returns its integer value. The example below shows the use of the parseInt() method to convert the String to integer.

4-5

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

import java.io.*; public class IntegerInput { public static void main(String[] args) throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in )); String numberOfstudent; int studNum;

System.out.print(Enter number of students:); numberOfstudent = input.readLine(); studNum = Integer.parseInt(numberOfstudent);

System.out.println(Total is + studNum); } }

The output of the program is shown below:

4-6

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

4.6 Converting String to Real


We can also convert a String to a double by using the Double class, which is also a part of java.lang package. A method of the Double class, which is parseDouble(), is used to convert the String to double. It takes the String argument and returns its double value. The example below shows the use of the parseDouble() method to convert the String to integer. import java.io.*; public class DoubleInput { public static void main(String[] args) throws Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in )); String feePayment; double payment;

System.out.print(Enter fees paid:); feePayment = input.readLine(); payment = Double.parseDouble(feePayment);

System.out.println(Payment made is + payment); } }

4-7

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

The output of the program is shown below:

4-8

CHAPTER 4: SIMPLE INPUT/OUTPUT OPERATIONS

4.7 Programming Exercises


1. Write a program which allows the user to enter his name, age and height. Use output statements to display the information that he had entered. You should use the correct method to convert the age to integer and height to real before displaying the information. 2. Write a program which allows the user to enter two numbers, multiply and then displays the result on the screen.

4-9

You might also like