Chapter 3 Java InputOutput Learning Java through Alice

  • Slides: 14
Download presentation
Chapter 3 Java Input/Output

Chapter 3 Java Input/Output

Learning Java through Alice © Daly and Wrigley 2 Objectives • Apply formats (currency

Learning Java through Alice © Daly and Wrigley 2 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.

Learning Java through Alice © Daly and Wrigley Scanner Class Input data from the

Learning Java through Alice © Daly and Wrigley Scanner Class Input data from the Java console: Value Returned Method Returns… byte next. Byte() Input as a byte. short next. Short() Input as a short. int next. Int() Input as an integer. long next. Long() Input as a long. float next. Float() Input as a float. double next. Double() Input as a double. boolean next. Boolean() Input as a boolean. String next() Next word as a String next. Line() Input line as a String. Note: In order to use this class, you must use the following import statement: import java. util. Scanner; 3

Learning Java through Alice © Daly and Wrigley Scanner Age Example: 4

Learning Java through Alice © Daly and Wrigley Scanner Age Example: 4

Learning Java through Alice © Daly and Wrigley Scanner Addition Example: 5

Learning Java through Alice © Daly and Wrigley Scanner Addition Example: 5

Learning Java through Alice © Daly and Wrigley Input Dialog Boxes An input dialog

Learning Java through Alice © Daly and Wrigley Input Dialog Boxes An input dialog box asks a question and uses a box for entering a response. String response = JOption. Pane. show. Input. Dialog(null, "Enter Fahrenheit"); Note: In order to use this class, you must use the following import statement: import javax. swing. JOption. Pane; 6

Learning Java through Alice © Daly and Wrigley Input Dialog Boxes An input dialog

Learning Java through Alice © Daly and Wrigley Input Dialog Boxes An input dialog box asks a question and uses a box for entering a response. String response = JOption. Pane. show. Input. Dialog(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. parse. Double(response); Note: In order to use this class, you must use the following import statement: import javax. swing. JOption. Pane; 7

Learning Java through Alice © Daly and Wrigley Message Dialog Boxes Uses a simple

Learning Java through Alice © Daly and Wrigley Message Dialog Boxes Uses a simple window to display (output) information. JOption. Pane. show. Message. Dialog(null, "Fahrenheit: " + fahrenheit + "n. Celsius: " + celsius); Note: In order to use this class, you must use the following import statement: import javax. swing. JOption. Pane; 8

Learning Java through Alice © Daly and Wrigley JOption. Pane Addition Example 9

Learning Java through Alice © Daly and Wrigley JOption. Pane Addition Example 9

Learning Java through Alice © Daly and Wrigley JOption. Pane Addition Output 10

Learning Java through Alice © Daly and Wrigley JOption. Pane Addition Output 10

Learning Java through Alice © Daly and Wrigley Message Icons JOption. Pane. show. Message.

Learning Java through Alice © Daly and Wrigley Message Icons JOption. Pane. show. Message. Dialog(null, "Warning to user. ", "Warning", JOption. Pane. WARNING_MESSAGE); JOption. Pane. show. Message. Dialog(null, "Bad Data. ", "Error", JOption. Pane. ERROR_MESSAGE); 11

Learning Java through Alice © Daly and Wrigley Message Icons Continued… Argument Icon -1

Learning Java through Alice © Daly and Wrigley Message Icons Continued… Argument Icon -1 No icon 0 1 2 3 Example: JOption. Pane. show. Message. Dialog(null, “Changing Icon. ", “Icon", 1); 12

Learning Java through Alice © Daly and Wrigley Number. Format Class • Used to

Learning Java through Alice © Daly and Wrigley Number. Format Class • Used to format output as currency. • Currency Example: Number. Format currency. Formatter = Number. Format. get. Currency. Instance( ); System. out. println(currency. Formatter. format(20. 5) ); Output: $20. 50 Note: In order to use this Number. Format class, you must use the following import statement: import java. text. Number. Format; 13

Learning Java through Alice © Daly and Wrigley Decimal. Format Class Used to format

Learning Java through Alice © Daly and Wrigley Decimal. Format Class Used to format output with a specific number of digits before and after the decimal point. Character 0 #. , % Meaning 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. double fahrenheit = 212. 5; Decimal. Format pattern. Formatter = new Decimal. Format ("#, ###. 00"); System. out. println(“The temperature is “ + pattern. Formatter. format(fahrenheit)); Output: The temperature is 212. 50 Note: In order to use this Number. Format class, you must use the following import statement: import java. text. Decimal. Format; 14