1 / 29

User Input

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

indra
Download Presentation

User Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. User Input JOptionPane Class EasyReader 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. Fran Trees

  3. Using JOptionPane • Advantages • no additional "outside" classes • part of Java language • Disadvantages • a bit cumbersome to get what we want. Fran Trees

  4. JOptionPane • JOptionPane.showDialog method prompts the user for an input string. • JOptionPane demonstrates the use of a graphical user interface. Fran Trees

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

  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. Fran Trees

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

  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. Fran Trees

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

  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. 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: "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

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

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

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

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

  19. EasyReader input = new EasyReader(); • Instantiates a new EasyReader object. 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(); Fran Trees

  21. System.out.print("Enter an integer for your FunNumber: "); • Simple prompt to user. 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(); Fran Trees

  23. int num = input.readInt(); • invokes the readInt method of the EasyReader object input. • this method returns an int. 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(); Fran Trees

  25. String line = input.readLine(); • consumes end of line 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 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. Fran Trees

  28. More on EasyReader • http://www.skylit.com/javamethods/appxe.html 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. Fran Trees

More Related