user input

29
Drew University Fran Trees 1 User Input JOptionPane Class EasyReader

Upload: indra

Post on 07-Jan-2016

70 views

Category:

Documents


1 download

DESCRIPTION

User Input. JOptionPane Class EasyReader. User input. Suppose we want to test our FunNumber class and Word class in JCreator. We want the user to input the String or number. Using JOptionPane. Advantages no additional "outside" classes part of Java language Disadvantages - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: User Input

Drew University Fran Trees 1

User Input

JOptionPane Class

EasyReader

Page 2: User Input

Drew University Fran Trees 2

User input

Suppose we want to test our FunNumber class and Word class in JCreator.

We want the user to input the String or number.

Page 3: User Input

Drew University Fran Trees 3

Using JOptionPane

Advantages no additional "outside" classes part of Java language

Disadvantages a bit cumbersome to get what we want.

Page 4: User Input

Drew University Fran Trees 4

JOptionPane

JOptionPane.showDialog method prompts the user for an input string.

JOptionPane demonstrates the use of a graphical user interface.

Page 5: User Input

Drew University Fran Trees 5

Using JOptionPaneimport javax.swing.JOptionPane; public class InputTest{ public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter an

integer for your FunNumber: ");

int num = Integer.parseInt(input); FunNumber n = new FunNumber(num);

System.out.println("The sum of the digits is :" + n.sumDigits());

System.exit(0); }}

Page 6: User Input

Drew University Fran Trees 6

import javax.swing.JOptionPane;

Swing toolkit is the most advanced user interface that Sun Microsystems has created for Java.

When we use this class, we must include this import statement in our program.

Page 7: User Input

Drew University Fran Trees 7

Using JOptionPaneimport javax.swing.JOptionPane; public class InputTest{ public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter an

integer for your FunNumber: ");

int num = Integer.parseInt(input); FunNumber n = new FunNumber(num);

System.out.println("The sum of the digits is :" + n.sumDigits());

System.exit(0); }}

Page 8: User Input

Drew University Fran Trees 8

String input = JOptionPane.showInputDialog("Enter an integer for your FunNumber: ");

JOptionPane.showInputDialog is a method that shows a dialog box with the indicated prompt and returns the String entered by the user.

Page 9: User Input

Drew University Fran Trees 9

Using JOptionPaneimport javax.swing.JOptionPane; public class InputTest{ public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter an

integer for your FunNumber: ");

int num = Integer.parseInt(input);

FunNumber n = new FunNumber(num); System.out.println("The sum of the digits is :" +

n.sumDigits());

System.exit(0); }}

Page 10: User Input

Drew University Fran Trees 10

int num = Integer.parseInt(input);

Often we want the input as a number, not a String. Integer.parseInt(input); Double.parseDouble(input);

will convert Strings to a numbers.

Page 11: User Input

Drew University Fran Trees 11

int num = Integer.parseInt(input);

Often we want the input as a number, not a String. Integer.parseInt(input); Double.parseDouble(input);

will convert Strings to a numbers. If the user doesn't type a number, an exception is thrown.

Exception in thread "main" java.lang.NumberFormatException: For input string: "asd" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:468) at java.lang.Integer.parseInt(Integer.java:518) at InputTest.main(InputTest.java:20)

Page 12: User Input

Drew University Fran Trees 12

Using JOptionPaneimport javax.swing.JOptionPane; public class InputTest{ public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter an

integer for your FunNumber: ");

int num = Integer.parseInt(input);

FunNumber n = new FunNumber(num); System.out.println("The sum of the digits is :" +

n.sumDigits());

System.exit(0); }}

Page 13: User Input

Drew University Fran Trees 13

System.exit(0);

Whenever you use JOptionPane in your programs, you need to add this line to the end of your main method. The showInputDialog method starts a user

input thread to handle user input The thread is still running when the main

method reaches the end.

Page 14: User Input

Drew University Fran Trees 14

Using EasyReader

a class to make input easy (easier). Advantages:

easy to use for string and numeric input Disadvantages

not part of Java classes must add to your projects.

Page 15: User Input

Drew University Fran Trees 15

EasyReader Method Summary

char readChar() Reads the next character from a file (any character including

a space or a newline character).  double readDouble()

Reads the next double (without validating its format)  int readInt()

Reads the next integer (without validating its format) String readLine()

Reads from the current position in the file up to and including the next newline character. java.lang.

String readWord() Skips whitespace and reads the next word (a string of

consecutive non-whitespace characters (up to but excluding the next space, newline, etc.) 

Page 16: User Input

Drew University Fran Trees 16

To open the standard input stream for reading keyboard input use: EasyReader console = new EasyReader();

//console is the name you give to the input stream (can be anything you like).

Using EasyReader

Page 17: User Input

Drew University Fran Trees 17

EasyReader sample program:

EasyReader input = new EasyReader();

System.out.print("Enter an integer for your FunNumber: ");

int num = input.readInt();

String line = input.readLine();

Page 18: User Input

Drew University Fran Trees 18

EasyReader sample program:

EasyReader input = new EasyReader();

System.out.print("Enter an integer for your FunNumber: ");

int num = input.readInt();

String line = input.readLine();

Page 19: User Input

Drew University Fran Trees 19

EasyReader input = new EasyReader();

Instantiates a new EasyReader object.

Page 20: User Input

Drew University Fran Trees 20

EasyReader sample program:

EasyReader input = new EasyReader();

System.out.print("Enter an integer for your FunNumber: ");

int num = input.readInt();

String line = input.readLine();

Page 21: User Input

Drew University Fran Trees 21

System.out.print("Enter an integer for your FunNumber: ");

Simple prompt to user.

Page 22: User Input

Drew University Fran Trees 22

EasyReader sample program:

EasyReader input = new EasyReader();

System.out.print("Enter an integer for your FunNumber: ");

int num = input.readInt();

String line = input.readLine();

Page 23: User Input

Drew University Fran Trees 23

int num = input.readInt();

invokes the readInt method of the EasyReader object input.

this method returns an int.

Page 24: User Input

Drew University Fran Trees 24

EasyReader sample program:

EasyReader input = new EasyReader();

System.out.print("Enter an integer for your FunNumber: ");

int num = input.readInt();

String line = input.readLine();

Page 25: User Input

Drew University Fran Trees 25

String line = input.readLine();

consumes end of line

Page 26: User Input

Drew University Fran Trees 26

Reading data from keyboard:

int n = input.readInt(); // reads an integer

double d = input.readDouble(); // reads a double

char ch = input.readChar(); // reads any character, // including whitespace

String word = input.readWord(); // reads a contiguous string of // non-whitespace characters

Page 27: User Input

Drew University Fran Trees 27

NOTE:

readInt, readDouble, readChar, and readWord methods do not consume the end of the line after reading the last item. Call readLine to get rid of the tail of the line (even if only the newline character is left) before calling readLine on the next line.

Page 28: User Input

Drew University Fran Trees 28

More on EasyReader

http://www.skylit.com/javamethods/appxe.html

Page 29: User Input

Drew University Fran Trees 29

Problem

Create a new empty JCreator workspace named Chapter 6.

Add to this a new Drew Basic Application project named InputTest.

In the InputTest folder (using Windows explorer) copy FunNumber.java and Word.java.

Write a test program to test these classes useing input from the user. Demonstrate use of JOptionPane and EasyReader.