DOC PREVIEW
UNI CS 4550 - Options for User Input

This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Options for User InputUsing the command lineSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8What are some of the problems with this solutionFixing this problemSlide 11I learned something new!Yes!What about?Slide 15Your turn to write a simple program using the command lineSimple Calculations at Command LineFirst Attempt - What’s wrong?Correct CalculateThe wrapper classesBut wait a minute!Lifetime ModifiersSlide 23Slide 24Slide 25Slide 26Homework #2 - Implementing ConstructorsHomework #2 - Task 1Homework #2 - Task 2Homework #2 - Task 3Homework #2 - Task 4Homework #2 - Task 5Homework #2 - Task 6Homework #2 - Sample ExecutionsSlide 35Slide 36Slide 37Slide 38Options for User Input•Options for getting information from the user–Write event-driven code•Con: requires a significant amount of new code to set-up•Pro: the most versatile.–Use System.in•Con: less versatile then event-driven•Pro: requires less new code–Use the command line (String[ ] args)•Con: very limited in its use•Pro: the simplest to set upUsing the command line•Remember what causes the “big bang” in our programs?Using the command line•Remember what causes the “big bang” in our programs?public static void main (String [] args) {Using the command line•Remember what causes the “big bang” in our programs?public static void main (String [] args) {•main expects an array of strings as a parameter.•The fact of the matter is that this array has been a null array in each of our programs so far.Using the command line•However, we can give this array values by providing command line arguments when we start a program running.Using the command line•However, we can give this array values by providing command line arguments when we start a program running.java MyProgram String1 String2 String3Using the command line•However, we can give this array values by providing command line arguments when we start a program running.$ java MyProgram String1 String2 String3 args[0] args[1] args[2]Using the command line•We can use this to get information from the user when the program is started:public class Echo {public static void main(String [] args) {System.out.println(“args[0] is “ + args[0]);System.out.println(“args[1] is “ + args[1]);} // end main} // end Echo class$ javac Echo.java$ java Echo Mark Fienupargs[0] is Markargs[1] is FienupWhat are some of the problems with this solution•This works great if we “behave” and enter two arguments. But what if we don’t?$ java Echo Mark Alan Fienupargs[0] is Markargs[1] is Alan (no problem, but Fienup gets ignored)$ java Echo Markargs[0] is MarkException in thread “main” java.lang.ArrayIndexOutOfBoundsException:(Big problem!)Fixing this problem•There are several ways to work around this problem–Use Java’s exception handling mechanism (not ready to talk about this yet)–Write your own simple check and handle it yourselfpublic class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main} // end MyEcho2Fixing this problempublic class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main} // end MyEcho2$ java MyEcho2 MarkUsage: java MyEcho2 string1 string2 $ java MyEcho2 Mark Alan FienupUsage: java MyEcho2 string1 string2I learned something new!•Will this code work if the user types NO arguments: “java MyEcho2”?public class MyEcho2 { public static void main( String[] args ) { if (args.length == 2) { System.out.println("args[0] is ” + args[0]); System.out.println("args[1] is " + args[1]); } else { System.out.println( "Usage: java MyEcho2 " + "string1 string2"); } // end if } // end main} // end MyEcho2Yes!•The args array reference could be “null”, so doing args.length would cause an error!•But it is not, since args is an array reference to an actual array with zero elements.What about?public class TestArray { public static void main( String[] args ) { System.out.println("args.length = " + args.length ); String[] temp0 = {}; System.out.println( "temp0.length = " + temp0.length ); String[] tempNull; System.out.println("tempNull.length=" + tempNull.length); } // end main} // end TestArray class$ javac TestArray.javaTestArray.java:7: variable tempNull might not have been initialized System.out.println( "tempNull.length = " + tempNull.length ); ^1 errorWhat about?public class TestArray { public static void main( String[] args ) { System.out.println("args.length = " + args.length ); String[] temp0 = {}; System.out.println( "temp0.length = " + temp0.length ); String[] tempNull = null; System.out.println("tempNull.length=" + tempNull.length); } // end main} // end TestArray class$ java TestArrayargs.length = 0temp0.length = 0Exception in thread "main" java.lang.NullPointerException at TestArray.main(TestArray.java:7)Your turn to write a simple program using the command line•Write a program to echo all command-line arguments to the System.out$ java EchoAll This is a long lineargs[0] is Thisargs[1] is isargs[2] is aargs[3] is longargs[4] is lineSimple Calculations at Command Line$ java Calculate 6 + 410$ java Calculate 8 - 53First Attempt - What’s wrong?public class Calculate { public static void main( String[] args ) { int operand1 = args[0]; String operator = args[1]; int operand2 = args[2]; if ( operator.equals("+") ) { System.out.println( operand1 + operand2 ); } else if ( operator.equals("-") ) { System.out.println( operand1 - operand2 ); } else { System.out.println("Invalid operator: " + operator); } // end if } // end main} // end Calculate$ javac Calculate.javaCalculate.java:3: incompatible typesfound : java.lang.Stringrequired: int int operand1 = args[0]; ^Correct Calculatepublic class Calculate { public static void main( String[] args ) { int operand1 = Integer.parseInt( args[0] ); String


View Full Document

UNI CS 4550 - Options for User Input

Download Options for User Input
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Options for User Input and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Options for User Input 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?