User Input
description
Transcript of User Input
![Page 1: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/1.jpg)
Drew University Fran Trees 1
User Input
JOptionPane Class
EasyReader
![Page 2: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/19.jpg)
Drew University Fran Trees 19
EasyReader input = new EasyReader();
Instantiates a new EasyReader object.
![Page 20: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/21.jpg)
Drew University Fran Trees 21
System.out.print("Enter an integer for your FunNumber: ");
Simple prompt to user.
![Page 22: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/25.jpg)
Drew University Fran Trees 25
String line = input.readLine();
consumes end of line
![Page 26: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/28.jpg)
Drew University Fran Trees 28
More on EasyReader
http://www.skylit.com/javamethods/appxe.html
![Page 29: User Input](https://reader036.fdocuments.net/reader036/viewer/2022062517/56813d5c550346895da72822/html5/thumbnails/29.jpg)
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.