0% found this document useful (0 votes)
319 views9 pages

Basic Java Chapter 4

This document discusses simple input and output in Java programs. It covers accepting character, string, integer and real number inputs from the user and displaying outputs. The key points are: - Character input uses System.in.read() and string input uses BufferedReader and InputStreamReader. - Integer input converts a string to an integer using Integer.parseInt() and real number input converts a string to a double using Double.parseDouble(). - Simple output uses print() and println() methods of System.out to display information to the standard output. - Programming exercises are provided to allow users to input name, age, height and perform number calculations.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
319 views9 pages

Basic Java Chapter 4

This document discusses simple input and output in Java programs. It covers accepting character, string, integer and real number inputs from the user and displaying outputs. The key points are: - Character input uses System.in.read() and string input uses BufferedReader and InputStreamReader. - Integer input converts a string to an integer using Integer.parseInt() and real number input converts a string to a double using Double.parseDouble(). - Simple output uses print() and println() methods of System.out to display information to the standard output. - Programming exercises are provided to allow users to input name, age, height and perform number calculations.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 9

Simple Input/Output

Chapter Objectives
Upon completion of this chapter, you will know

 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
Simple Input Command
A program that accepts values at run time is interactive because it exchanges
communications, or interacts with the 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.
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 Java’s 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.
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( "\n The character entere is: " + charInput );


}
}
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 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 strStudentName;

System.out.print( "Enter student name: " );


strStudentName = input.readLine();

System.out.println( "Your Name is: " + strStudentName );


}
}
The example show 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 java.io package. Thus, at the beginning
of the program, the java.io package is imported into the program.
Simple Output Command
In Java Programming language, the terminal window is used as the standard
output. System.out is 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.
Converting String to Integers
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 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.
import java.io.*;

public class IntegerInput


{
public static void main( String[] args ) throws Exception
{
BufferedReader input = new BufferedReader( new
InputStreamReader( System.in ) );
String numberOfStudents;
int studNum;

System.out.print( "Enter a number of students: " );


numberOfStudents = input.readLine();
studNum = Integer.parseInt( numberOfStudents );

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


}
}
Conversion 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.
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( "Total is " + payment );


}
Programming Exercise

Write a program which allows the user to enter his name, age and height. Use
Output statement to display the information that he had entered. You should use
the correct method to convert the age to integer and height to real number before
displaying the information.

Write a program which allows the user to enter two number, multiply and the
displays the result on the screen.

You might also like