Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 201 Lecture 8: John Hurley CS201 Cal State LA Dialog Boxes – GUI construction is taught in CS202, but we will cover a few GUI rudiments in 201 – The first is the JOptionPane class, which provides pop-up I/O boxes of several kinds – Need to include this at the very top of your class: • import javax.swing.JOptionPane; – JOptionPane.showMessageDialog() • displays a dialog box with text you specify Dialog Boxes import javax.swing.JOptionPane; public class DialogBox{ public static void main(String[] args){ for(int i = 0; i < 4; i++) { JOptionPane.showMessageDialog(null, "This is number " + i); } } } Dialog Boxes – JOptionPane.showInputDialog() shows a dialog box that can take input – The input is a String – We will learn soon how to parse from String to other types Dialog Boxes import javax.swing.JOptionPane; public class InputBox{ public static void main(String[] args){ String input = JOptionPane.showInputDialog(null, "Please enter some input "); JOptionPane.showMessageDialog(null, "You entered: \"" + input + "\""); String input2 = input.concat(" " + JOptionPane.showInputDialog(null, "Please enter some more input ")); JOptionPane.showMessageDialog(null, "You entered: \"" + input2 + "\""); } } Casting Strings to Numeric Types • Recall that input from JOptionPane.showInputDialog is a String • Cast to integer: Integer.parseInt(intString); – Example: – String input = • int age = Integer.parseInt(JOptionPane. showInputDialog(null, “Please enter your age”); • Cast to double: Double.parseDouble(doubleString); Casting to Numeric Types • Note the capitalization in the method names. Double and Integer are not quite the same as double and integer. – You’ll understand when you are a little older… Parsing to Integer import javax.swing.JOptionPane; public class NumericCastDemo{ public static void main(String[] args){ int age = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your age")); if(age < 30) JOptionPane.showMessageDialog(null, age + " is pretty young."); else if(age > 100) JOptionPane.showMessageDialog(null, "really?"); else JOptionPane.showMessageDialog(null, "That's OK. Methuseleh lived to be " + (270 - age) + " years older than you are now."); } // end main() } // end class Parsing to Double import javax.swing.JOptionPane; public class NumericCastDemo{ public static void main(String[] args){ double age = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter your age")); if(age < 30) JOptionPane.showMessageDialog(null, age + " is pretty young."); else if(age > 100) JOptionPane.showMessageDialog(null, "really?"); else JOptionPane.showMessageDialog(null, "That's OK. Methuseleh lived to be " + (270 / age) + " times as old as you are now."); } // end main() } // end class Imports – We have already discussed javax.swing.JOptionPane methods to show input and message dialogs – These required the following line at the top of the class: • Import javax.swing.JOptionPane; – If you omit this line, you will get an error message like this: SwitchDemo.java: 7: cannot find symbol Symbol:variable JOptionPane Imports –Java classes are organized in packages • Late in this class or early in CS202 you will start using packages for multiple classes –javax.swing is a package of GUI-related classes that is included with all distributions of Java –JOptionPane is a class within this package –JOptionPane.showMessageDialog() and JOptionPane.showInputDialog are methods of the class Imports – Including a package in your program adds a small cost, slowing down the compiler as it looks through the imported package – Thus, things like javax.swing are not included automatically; you must specify that you need them – You will eventually be importing your own packages, too. – Don’t leave unused imports in your code Convert Character and Numbers to Strings The String class provides several static methods for converting characters, arrays of characters, and numeric values to Strings. These methods are named valueOf() with different argument types char, char[], double, long, int, and float. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’. 13 public class ValueOfDemo{ public static void main(String[] args){ double d = 1234.56789; // this would cause an error: int precision=d.indexOf('.'); String stringD = String.valueOf(d); int stringLength = stringD.length(); int locationOfDecimalPoint = stringD.indexOf("."); System.out.println("length of string " + stringD + " is " + stringLength + " location of decimal point is " + locationOfDecimalPoint); int precision = stringLength - locationOfDecimalPoint -1; System.out.println(precision + " digits after the decimal point"); } } Validating Data Type – We have already discussed how to make sure that numeric input from Scanner is within a desired range int age; do { System.out.println("How old are you?"); age = sc.nextInt(); } while (age < 0 || age > 100); Validating Scanner Input – So far, our programs have crashed if casts to numeric types failed: – Try this with input “two point five”: import java.util.Scanner; public class ReadDouble2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Double stuff = 0.0; do { System.out.print("Input a double. Enter 0 to quit:"); stuff = input.nextDouble(); System.out.println("\nYou entered: " + stuff); } while (stuff != 0.0); } Validating Scanner Input – Here is the simplest way to validate Scanner input for data type (that is, to make sure you get input that can be cast to double, integer, etc.) – Scanner has hasNext…() methods that see if there is a parseable token • hasNextInt() • hasNextDouble() • hasNextBoolean() – Also need next() to skip over bad input Validating Scanner Input – Try this one with input “nine.three”, then with input “nine point three: import java.util.Scanner; public class ReadDouble3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Double inpDouble = 10.0; // bad code ahead!! do { System.out.print("Input a double. Enter 0 to quit:"); if(input.hasNextDouble()){ inpDouble = input.nextDouble(); System.out.println("\nYou entered: " + inpDouble); } else input.next(); } while (inpDouble != 0.0); } } Validating Scanner Input – In order to get good output, we need to arrange things in a slightly more complex way: import java.util.Scanner; public class ReadInt{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int x; System.out.println("Enter an integer"); while (!sc.hasNextInt()) { sc.nextLine(); System.out.println("No, an integer!"); } x = sc.nextInt(); System.out.println(x); } } Validating Both Type and Value – In order to get good output, we need to arrange things in a slightly more complex way: Scanner sc = new Scanner(System.in); int value = 0; do { System.out.println("Enter an integer greater than 100"); while (!(sc.hasNextInt())) { sc.nextLine(); System.out.println("no, an integer!"); } value = sc.nextInt(); if(value <= 100) System.out.println("No, an integer greater than 100"); } while (value <= 100); System.out.println(value); Documentation From Oracle – Java was originally developed by Sun Microsystems and was closed-source but free for many years – Java is now open-source – Anyone is free to use it or to write compilers, virtual machines, etc. that implement it – Oracle bought Sun a couple of years ago and now is the champion of Java Documentation From Oracle – When you need a reference source for something like Scanner, look it up in Oracle’s excellent online documentation – Example: Google +Java +Scanner • Follow the link to http://doc.java.sun.com/DocWeb/api/java.util.Scann er