user input

Post on 07-Jan-2016

70 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

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

Drew University Fran Trees 1

User Input

JOptionPane Class

EasyReader

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.

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.

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.

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); }}

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.

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); }}

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.

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); }}

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.

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)

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); }}

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.

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.

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.) 

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

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();

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();

Drew University Fran Trees 19

EasyReader input = new EasyReader();

Instantiates a new EasyReader object.

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();

Drew University Fran Trees 21

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

Simple prompt to user.

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();

Drew University Fran Trees 23

int num = input.readInt();

invokes the readInt method of the EasyReader object input.

this method returns an int.

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();

Drew University Fran Trees 25

String line = input.readLine();

consumes end of line

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

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.

Drew University Fran Trees 28

More on EasyReader

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

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.

top related