chapter 3 java input/output. learning java through alice © daly and wrigley objectives apply...

14
Chapter 3 Java Input/Output

Upload: christopher-gulliver

Post on 02-Apr-2015

234 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Chapter 3

Java Input/Output

Page 2: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Objectives•Apply formats (currency and percentages) to output. •Use keyboard input in Java program to create interactivity. •Use dialog box input/output in Java program to create

interactivity. •Associate import statements with corresponding

input/output/format classes/packages.

2

Page 3: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Scanner ClassInput data from the Java console:

Note: In order to use this class, you must use the following import statement:

import java.util.Scanner;

Value Returned Method Returns…

byte nextByte() Input as a byte.

short nextShort() Input as a short.

int nextInt() Input as an integer.

long nextLong() Input as a long.

float nextFloat() Input as a float.

double nextDouble() Input as a double.

boolean nextBoolean() Input as a boolean.

String next() Next word as a String.

String nextLine() Input line as a String.

3

Page 4: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Scanner Age Example:

4

Page 5: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Scanner Addition Example:

5

Page 6: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Input Dialog BoxesAn input dialog box asks a question and uses a box for enteringa response.

String response = JOptionPane.showInputDialog(null, "Enter Fahrenheit");

Note: In order to use this class, you must use the following import statement:

import javax.swing.JOptionPane;

6

Page 7: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Input Dialog BoxesAn input dialog box asks a question and uses a box for enteringa response.

String response = JOptionPane.showInputDialog(null, "Enter fahrenheit number");

If this data is to be used in a calculation, it will need to be converted from String data to double

data or integer data with one of the following statements:

double fahrenheit = Double.parseDouble(response);

Note: In order to use this class, you must use the following import statement:

import javax.swing.JOptionPane;

7

Page 8: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Message Dialog BoxesUses a simple window to display (output) information.

JOptionPane.showMessageDialog(null, "Fahrenheit: " + fahrenheit + "\nCelsius: " + celsius);

Note: In order to use this class, you must use the following import statement:

import javax.swing.JOptionPane;

8

Page 9: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

JOptionPane Addition Example

9

Page 10: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

JOptionPane Addition Output

10

Page 11: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Message Icons

JOptionPane.showMessageDialog(null, "Warning to user. ", "Warning", JOptionPane.WARNING_MESSAGE);

JOptionPane.showMessageDialog(null, "Bad Data. ", "Error", JOptionPane.ERROR_MESSAGE);

11

Page 12: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

Message Icons Continued…Argument Icon

-1 No icon

0

1

2

3

Example:JOptionPane.showMessageDialog(null, “Changing Icon. ", “Icon", 1);

12

Page 13: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

NumberFormat Class• Used to format output as currency.  • Currency Example: 

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance( );

System.out.println(currencyFormatter.format(20.5) );

Output: $20.50

Note: In order to use this NumberFormat class, you must use the following import statement:

import java.text.NumberFormat;

13

Page 14: Chapter 3 Java Input/Output. Learning Java through Alice © Daly and Wrigley Objectives Apply formats (currency and percentages) to output. Use keyboard

Learning Java through Alice © Daly and Wrigley

DecimalFormat ClassUsed to format output with a specific number of digits before

and after the decimal point.  

double fahrenheit = 212.5;DecimalFormat patternFormatter = new DecimalFormat ("#,###.00");System.out.println(“The temperature is “ + patternFormatter.format(fahrenheit)); Output: The temperature is 212.50

Note: In order to use this NumberFormat class, you must use the following import statement:

import java.text.DecimalFormat;

Character Meaning

0 Required digit. Print 0 if there isn’t a digit in this place.

# Optional digit. Leave blank if there isn’t a digit in this place.

. Decimal point separator., Comma separator.

% Multiplies the result by 100 and displays a percent sign.

14