Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.

Similar presentations


Presentation on theme: "CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1."— Presentation transcript:

1 CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1

2 SUMMARY OF TECHNIQUES FOR RECEIVING INPUT FROM USER There are 3 techniques that we will discuss: – Using the command line This technique is used when we start the execution of the program. We use it to provide a set of inputs right after the command to the java interpreter using a DOS window. i.e. >java MyProgram 2 2.3 Nick 3 inputs are provided here the value 2, the value 2.3 and Nick. The program will capture the 3 inputs always as Strings 2

3 Using the command line to provide input The program has to be written as follows: public class MyProgram { public static void main(String[] args) { //write the code to capture the first input from the //command line – in our example the value 2 (as String) String a=args[0]; // because the value is expected to be an int data type and //not a String we need to convert the String to an int by //parsing it (using the Integer wrapper class) 3

4 Using the command line to provide input int x=Integer.parseInt(a); //The int 2 is now stored in x // now proceed to capture the second input String b=args[1]; // Now the value 2.3 is stored as a String in b // Since we expect it to be a double we need to convert //(parse) the String to a double data type double y=Double.parseDouble(b); // The double 2.3 is now stored in y // now proceed to capture the third input value String c=args[2]; // The value Nick is store din c. No need to parse it since we expect it to be a //String. other lines of code as needed by the program to process the above inputs 4

5 Using the command line to provide input Drawbacks of this technique – The inputs are fixed in value and also as far as the number of inputs. They can’t be changed once the program starts execution using this technique (you can change the inputs using a different technique i.e Scanner). – The inputs are always captured as Strings. We need to know the data type involved in order to parse it from a String to that data type. 5

6 Using the Scanner Object – Using the Scanner to capture input from keyboard This technique is used after the program starts execution. Some times we want some lines of code to be executed first and then we want the program to stop and wait for the user to type on the keyboard input values. The program needs to prompt the user as to when to start typing on the keyboard. The program needs to notify the user if a line of data is to be typed or if one data type at a time needs to be typed. The program resumes execution and captures the input typed when the user presses the Enter key. 6

7 Using the Scanner Object Example Import java.util.Scanner; public class MyProgram { public static void main(String[] args) { //assume that some lines of code that do something are written first //now we want to have the user enter some input values from the //keyboard. //First let us create a Scanner object and tell it to monitor the keyboard Scanner scan=new Scanner(System.in) //System.in means monitor the keyboard. 7

8 Using the Scanner Object //Next let us notify the user to enter dat System.out.println(“Please enter your age as an integer number”); // The program stops execution and waits for the user to type the //data and press Enter. The user can type a number like 19 and then //presses Enter. // The program needs to capture the value entered by the user. Since we //expect an int data type we would capture it as an int int x= scan.nextInt(); //now the value 19 is stored in location x. We can continue with other code to process the input captured. Notice that the Scanner class can capture other data types (see the API). 8

9 Using the JOptionPane Object – Using the JOptionPane to capture input from keyboard This technique uses a Graphical presentation to the user. A box appears on the screen and the user types the input on that box. The user presses the OK button of the Box and the program capture s the input typed. Although it is beyond the scope of the course we will do it in the practice exercise 6. You will be given the code to use. This technique is used to capture input from the user after the program starts execution. It is similar to using the scanner except that the instructions to the user and the output of the program can be presented on a graphical component called a box, instead on a DOS window. 9


Download ppt "CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1."

Similar presentations


Ads by Google