310 likes | 644 Views
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
E N D
User Input JOptionPane Class EasyReader Fran Trees
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. Fran Trees
Using JOptionPane • Advantages • no additional "outside" classes • part of Java language • Disadvantages • a bit cumbersome to get what we want. Fran Trees
JOptionPane • JOptionPane.showDialog method prompts the user for an input string. • JOptionPane demonstrates the use of a graphical user interface. Fran Trees
Using JOptionPane import 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); } } Fran Trees
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. Fran Trees
Using JOptionPane import 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); } } Fran Trees
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. Fran Trees
Using JOptionPane import 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); } } Fran Trees
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. Fran Trees
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: "a sd" 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) Fran Trees
Using JOptionPane import 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); } } Fran Trees
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. Fran Trees
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. Fran Trees
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.) Fran Trees
Using EasyReader 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). Fran Trees
EasyReader sample program: EasyReader input = new EasyReader(); System.out.print("Enter an integer for your FunNumber: "); int num = input.readInt(); String line = input.readLine(); Fran Trees
EasyReader sample program: EasyReader input = new EasyReader(); System.out.print("Enter an integer for your FunNumber: "); int num = input.readInt(); String line = input.readLine(); Fran Trees
EasyReader input = new EasyReader(); • Instantiates a new EasyReader object. Fran Trees
EasyReader sample program: EasyReader input = new EasyReader(); System.out.print("Enter an integer for your FunNumber: "); int num = input.readInt(); String line = input.readLine(); Fran Trees
System.out.print("Enter an integer for your FunNumber: "); • Simple prompt to user. Fran Trees
EasyReader sample program: EasyReader input = new EasyReader(); System.out.print("Enter an integer for your FunNumber: "); int num = input.readInt(); String line = input.readLine(); Fran Trees
int num = input.readInt(); • invokes the readInt method of the EasyReader object input. • this method returns an int. Fran Trees
EasyReader sample program: EasyReader input = new EasyReader(); System.out.print("Enter an integer for your FunNumber: "); int num = input.readInt(); String line = input.readLine(); Fran Trees
String line = input.readLine(); • consumes end of line Fran Trees
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 Fran Trees
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. Fran Trees
More on EasyReader • http://www.skylit.com/javamethods/appxe.html Fran Trees
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. Fran Trees