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 7: John Hurley Summer 2012 Cal State LA Programming Errors Syntax Errors Incorrect Java. An IDE will warn you about these, but at a command line, they would be detected by the compiler; also called "compiler errors" Runtime Errors Cause the program to abort; in CS202 you will learn how to manage many of these to avoid program crashes. Logic Errors Produce incorrect results 2 Syntax Error public class Errors{ public static void main(String[] args){ int i = 1 System.out.println(i); } } 3 Runtime Error public class Errors{ public static void main(String[] args){ int y = 10; for(int x = 0; x <=10; x++){ y -= 1; System.out.println(10 / y); } } } 4 Logic Error public class PowerError{ public static void main(String[] args){ int answer = 1; for(int power = 0; power <= 10; power++){ answer = answer * 2; System.out.println("2 ^ " + power + " = " + answer); } } 5 } Test Your Work It is easy to test our programs in which there is only one sequence the computer can follow: public class Power{ public static void main(String[] args){ int answer = 1; for(int power = 0; power <= 10; power++){ System.out.println("2 ^ " + power + " = " + answer); answer = answer * 2; } } } Test Your Work At this point, we are writing programs that take user input Users are human beings The next slide shows a statistically representative sample of human beings Test Your Work Program execution becomes more complex when unpredictable human beings intervene Make sure to test thoroughly The success rate of untested programs is close to 0 We have it easy. Testing involves saving, compiling, and running on a machine that is at our fingertips; it hasn’t always been that way. 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 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 nextLine() 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: Scanner sc = new Scanner(System.in); int x; String badInputString = null; System.out.println("Enter an integer"); while (!sc.hasNextInt()) { badInputString = sc.nextLine(); + " isn't an integer! Please try } x = sc.nextInt(); System.out.println(x); System.out.println(badInputString again."); Validating Both Type and Value int value = 0; String badInputString = null; do { System.out.println("Enter an integer greater than 100"); while (!(sc.hasNextInt())) { badInputString = sc.nextLine(); System.out.println(badInputString + " isn't an integer! Please try again."); } value = sc.nextInt(); sc.nextLine(); // throw awqay linefeed } while (value <= 100); System.out.println(value); 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.Scanner