Chapter 3 - Input Output Data
Chapter 3 - Input Output Data
1
Objectives
At the end of this chapter, student should be able to:
– understand the concept of input and output representation in
java.
– obtain input from the console using the Scanner class.
– obtain input using the JOptionPane input dialog boxes
– examine ways to output results using output statements.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
2
Program Without Input
public class WithoutInput {
public static void main(String[] args){
System.out.println("How old are you?");
System.out.println("Hi!!, you are 20 years
old");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
3
Program With Static Input
public class UsingStaticInput {
public static void main(String[] args){
int age;
System.out.print("How old are you?");
age = 20;
System.out.println("Hi!!, you are "+age+ " years
old");
}
}
How old are you?
Hi!!, you are 20 years Old
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
4
Input
Input can be obtained from:
– console
– input dialog
To read input from the console, Java uses System.in to
refer to the standard input device.
By default, the input device is the keyboard.
When the computer gets the data from the keyboard, the
user is said to be acting interactively.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
5
Reading Input from the Console
Scanner class can be used to create an object to read input from
System.in.
Scanner is a predefined Java class.
The Scanner class is in the java.util package
To read data:
– Create an input stream object of the class Scanner and associate
it with the standard input device as the following statements:
Scanner console = new Scanner(System.in);
– Use the methods such as next(), nextLine(), nextInt(),
nextShort(), nextByte(), nextLong(), nextFloat()
and nextDouble()
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
6
Structure of a program
//Java Package import java.lang.*;
//class public class Welcome{
//main method public static void
main(String[] args){
//open program, {
//body System.out.println(“Welc
ome”);
//close program, } }
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
7
Implicit Import and Explicit
Import
java.util.* ; // Implicit import
java.util.Scanner; // Explicit
Import
No performance difference
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
8
Methods for Scanner Object
Methods Description
nextByte() reads an integer of the byte type.
nextShort() reads an integer of the short type.
nextInt() reads an integer of the int type.
nextLong() reads an integer of the long type.
nextFloat() reads a number of the float type.
nextDouble() reads a number of the double type.
next() reads a string that ends before a whitespace character.
nextLine() reads a line of text (i.e., a string ending with the Enter key
pressed).
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
9
Using Scanner Class for Input
age 17
import java.util.*;
public class UsingScannerClass {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int age;
System.out.print("How old are you?");
age = input.nextInt();
System.out.println("Hi!!, you are "+age+ " years
old");
}
} How old are you? 17
Hi!!, you are 17 years old
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
10
Exercise
Write the java program using the given pseudocode.
1. Start
2. Get a radius
3. Calculate area where area = x r x r
4. Display area
5. End
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 11
Reading Input from the Console
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double value. For
example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
ComputeAreaWithConsoleInput Run
ComputeAverage Run
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
12
Getting Input from Input Dialogs
invoking the JOptionPane.showInputDialog
method
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
13
Getting Input from Input Dialogs
Two ways to invoke showInputDialog method:
– JOptionPane.showInputDialog(x);
where x is a string for the prompting message.
– String string =
JOptionPane.showInputDialog(null, x, y,
JOptionPane.QUESTION_MESSAGE);
where x is a string for the prompting message and y is a string for the
title of the input dialog box, as shown in the example below.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
14
Output
In Java, output on the standard output device is
accomplished by using the standard output object
System.out.
The object System.out has access to two methods, to
output a string on the standard output device:
– print
– println
– printf: print and format the output
Exercise: open Java API and understand the methods
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
15
Output
Syntax:
System.out.print(expression);
System.out.println(expression);
System.out.println();
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
16
Output
The expression is evaluated, and its value is printed at the
current insertion point on the output device.
The method print() leaves the insertion point after the last
character of the value of expression,
The method println() positions the insertion point at the
beginning of the next line.
The statement System.out.println(); only positions the
insertion point at the beginning of the next line.
The method printf() produces output in a specific format
and leaves the the insertion point after the last character of the
value of expression
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
17
Output
In an output statement, if expression consists of only one string
or a single constant value, then expression evaluates to itself.
If expression consists of only one variable, then expression
evaluates to the value of the variable. When an output statement
outputs
char ch=‘A’;
System.out.println(ch); A
System.out.println('A'); A
System.out.printf("Hello %s!%n", "World");
Hello World!
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
18
Output
public class Hello {
public static void main(String[] args){
System.out.println(“Hello World”);
System.out.println(“Welcome to Java “);
System.out.printf(“%s”, “Hello World”);
}
}
C:\> javac Hello.java
C:\> java Hello
Hello World
Welcome to Java
Hello World
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
19
public class Hello {
Output
public static void main(String[] args){
System.out.println(29 / 4);
7
System.out.println("Hello there.");
Hello there.
System.out.println(12);
12
System.out.println("4 + 7");
System.out.println(4 + 7);
4+7
11
System.out.println('A');
A
System.out.println("4 + 7 = " + (4 + 7));
4 + 7 = 11
System.out.println(2 + 3 * 5);
17
System.out.println("Hello \nthere.");
Hello
}
there.
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
20
Exercises
1.
2.
3.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
21
Escape Character
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
22
Escape Character
public class Hello {
public static void main(String[] args){
System.out.print(“Hello World!! n Welcome to Java“);
System.out.println(“Hello World!!);
System.out.print(“\n Welcome to Java“);
}
}
Hello World!!
Welcome to Java
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
23
Escape Character
public class Escape_Character {
public static void main(String[] args){
System.out.println("The newline escape sequence is \\n");
System.out.println("The string \“Java\"");
System.out.println("The tab character is \'\\t\'");
}
}
What are
the
outputs?
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
24
The showMessageDialog
Method
JOptionPane.showMessageDialog(null,
"Welcome to Java!",
"Example 1.2 Output",
JOptionPane.INFORMATION_MESSAGE));
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 25
Two Ways to Invoke the Method
There are several ways to use the showMessageDialog
method. For the time being, all you need to know are two ways
to invoke it.
– use a statement as shown in the example:
JOptionPane.showMessageDialog(null, x, y,
JOptionPane.INFORMATION_MESSAGE));
where x is a string for the text to be displayed, and y is a string for
the title of the message dialog box.
– The other is to use a statement like this:
JOptionPane.showMessageDialog(null, x);
where x is a string for the text to be displayed.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 26
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;
name = JOptionPane.showInputDialog("What is Your Name?");
JOptionPane.showMessageDialog(null,"Hi!!! "+name+
"\nWelcome\nTo\nJava\
nWorld");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 27
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;
name = JOptionPane.showInputDialog("What
is Your Name?");
JOptionPane.showMessageDialog(null,
"Hi!!! "+name+"\nWelcome\nTo\nJava\nWorld");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 28
Example
import javax.swing.JOptionPane;
public class Welcome {
public static void main(String[] args) {
String name;
name = JOptionPane.showInputDialog("What is
Your Name?");
JOptionPane.showMessageDialog(null,"Hi!!!
"+name+ "\nWelcome\nTo\nJava\nWorld");
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 29
Common Pitfall 1: Redundant
Input Objects
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
30
Exercises
1. Write the java program using the given pseudocode.
Please use dialog box (input dialog+ message dialog) approach;
1. Start
2. Get year_of_birth
3. Calculate age where age = 2021 - year_of_birth
4. Display age
5. End
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved. 31
2. Write a program that reads a number in
feet, converts it to meters, and displays the
result. One foot is 0.305 meter. Here is a
sample run:
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All
rights reserved.
32