Docsity

Java Programming: Input, Formatting and User-Input Validation, Slides of Applications of Computer Sciences

Partial preview of the text

Download Java Programming: Input, Formatting and User-Input Validation and more Slides Applications of Computer Sciences in PDF only on Docsity!

Chapter 4:

Input and Formatting

Class Methods

Object-Oriented Program

Development Using Java: A

Class-Centered Approach

Objectives

  • Interactive Keyboard Input
  • Interactive Dialog Input
  • Creating a Class Library
  • Formatted Output
  • Mathematical Methods
  • Common Programming Errors

Interactive Keyboard Input

(continued)

  • End-of-file (EOF) marker:
    • Special end-of-data value
    • Numerical value that cannot be converted into a legitimate character value
  • If you would like to read an entire line at once:
    • Use supporting classes:
      • InputStreamReader
      • BufferedReader

Interactive Keyboard Input

(continued)

• BufferedReader:

  • Automatically constructs a string from character values provided by the InputStreamReader object
  • BufferedReader br = new BufferedReader(isr);

• A display prompt asks the user to enter data

• Calling readLine() puts system in wait state

until the user types data

The StringTokenizer Class

  • Token
    • String of characters separated by delimiting character
  • Delimiting characters
    • Whitespace by default in Java
  • Parsing the string
    • Separating individual tokens from string
  • Class StringTokenizer
    • Used to parse strings

User-Input Validation

  • Consists of:
    • Validating entered data either during or immediately after data have been entered
    • Providing the user with a way of reentering any invalid data
  • To handle invalid input, provide error processing

code

Interactive Dialog Input

  • GUI method of entering user data:
    • Method named showInputDialog()
    • In JOptionPane class
    • Creates dialog box that permits user to enter string at terminal
  • Syntax:
    • JOptionPane.showInputDialog(string);
  • Example:
    • s = JOptionPane.showInputDialog("Enter a number:");

Exception Handling

(continued)

  • Two fundamental types of errors:
    • Result from inability of program to obtain required resource
    • Result from flawed code
  • Checked exception:
    • Java checks that exceptions will be handled
    • Program must throw or handle exception

Exception Handling

(continued)

  • Unchecked exception:
    • Java compiler does not check for handling code
    • Error can always be prevented by programming