Reading input from the console input. Java's console input The console is the terminal window that...

27
Reading input from the console input

description

Java's console input When a Java program starts running, the Java runtime system will initialize many variables in support for the running program. One of these variables is the Java system variable: which represents the console input The variable System.in is included in every Java program (you don't need to define it). System.i n

Transcript of Reading input from the console input. Java's console input The console is the terminal window that...

Page 1: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading input from the console input

Page 2: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's console input

• The console is the terminal window that is running the Java program

I.e., that's the terminal window where you type in the command java ProgramName

Page 3: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's console input

• When a Java program starts running, the Java runtime system will initialize many variables in support for the running program.

One of these variables is the Java system variable:

which represents the console input

The variable System.in is included in every Java program (you don't need to define it).

System.in

Page 4: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's console input

• A Java program can obtains inputs from the console through the keyboard

• In other words:

• The Java system variable System.in represents the keyboard

Page 5: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

A note on the notation "System.in"

• At this moment in the course, we want to learn how to read input from the keyboard

All you need to know is:

• It is too early in the course to explain the notation System.in • We will explain this after we have covered classes

• The variable named System.in represents the keyboard  

Page 6: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's Scanner library functions

• Fact:

• The details of what the computer must do to read in a number will be discussed in CS255

• The Java programming language provides a collection of methods stored in the Scanner class that perform read operations      

(Remember that a class is a container for methods)

• There is a lot of work that the computer must do to read in a floating point number

Page 7: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's Scanner library functions (cont.)

• Webpage of the Java documentation on Scanner class: http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html

Page 8: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Java's Scanner library functions (cont.)

• We will now learn how to use the methods in the Scanner class to read in floating point numbers

Page 9: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Importing the Scanner class definition

• Recall the Rule of usage of methods in the Java library: (See: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/java-lib.html)

• If a Java program wants to use a method in the Java library, the Java program must first import the containing class

• All classes in the java.lang package have already been imported into a Java program

(You can use methods in these classes without the import clause)

Page 10: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Importing the Scanner class definition (cont.)

• We can use the following import clause to import the Scanner class:

import java.util.Scanner;

Page 11: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Preparation before we can read input from the keyboard

• Before a Java program can read input from the keyboard, the program must " construct a Scanner object

It is too early to explain what this means... I will only tell youhow to do it

Page 12: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Preparation before we can read input from the keyboard (cont.)

• A Scanner object is constructed using the following statement:

The name varName is an identifier

Example: constructing a Scanner object named in

Scanner varName = new Scanner(System.in);

Scanner in = new Scanner(System.in);

Page 13: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading in a floating point number from the keyboard

• After having constructed the Scanner object named in, you can use the following expression to read a floating point number from the keyboard:

You must save (store) the number read in by "in.nextDouble()" in a variable with an assignment statement

in.nextDouble()

Page 14: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading in a floating point number from the keyboard (cont.)

• What happens inside the computer:

• Just like Math.sqrt(..), the method call in.nextDouble() will invoke (run) a method in Java's library.

The task performed by in.nextDouble() is to read a floating point number from the keyboard:

Page 15: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading in a floating point number from the keyboard (cont.)

If you type in "3.5" on the keyboard at the time that in.nextDouble() is running, then the call will return the value 3.5

• The return value will replace the method call:

The input value 3.5 is then stored in the variable a !!!

Page 16: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Summary: steps to read in a floating point number

• This figure summarizes the programming steps to read in a floating point number:

Page 17: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Example: reading input for the a,b,c-formula • Programming Example: ABC formula

import java.util.Scanner; // Import Scanner class (contains methods

// for reading keyboard input)  public class Abc2 { public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable  Scanner in = new Scanner(System.in); // Construct a Scanner object  a = in.nextDouble(); // Read in next number and store in a b = in.nextDouble(); // Read in next number and store in b c = in.nextDouble(); // Read in next number and store in c

Page 18: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading in a floating point number from the keyboard (cont.)

x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);  System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); } }

Page 19: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading in a floating point number from the keyboard (cont.)

• Example Program: (Demo above code)    – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Abc2.java 

• How to run the program:

• Right click on link and save in a scratch directory

• To compile:   javac Abc2.java

• To run:          java Abc2

Page 20: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Good programming practice: Prompting user for input

• The previous program works, but requires the users to know exactly what to do

In other words:

• An unaware user may not know that he/she needs to enter some input before the program can perform its task.

Page 21: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Good programming practice: Prompting user for input (cont.)

• Good programming courtesy:

• When the program needs the user to enter input from the keyboard, it must print out a (short) prompt message

Page 22: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Good programming practice: Prompting user for input (cont.)

• Example

import java.util.Scanner; // Import Scanner class (contains methods // for reading keyboard input) public class Abc2 { public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable Scanner in = new Scanner(System.in); // Construct a Scanner object

Page 23: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Good programming practice: Prompting user for input (cont.)

System.out.print("Enter a = "); // ******* Prompt message a = in.nextDouble(); // Read in next number and store in a System.out.print("Enter b = "); b = in.nextDouble(); // Read in next number and store in b System.out.print("Enter c = "); c = in.nextDouble(); // Read in next number and store in c x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a); System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); } }

Page 24: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading other types of input from the keyboard

• The procedure to read other types of inputs from the keyboard is similar to the one above:

Page 25: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading other types of input from the keyboard (cont.)

• The only different is that we need to use a different method in the Scanner class that read the correct type of data.

Page 26: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading other types of input from the keyboard (cont.)

• Reading an integer number from the keyboard: use nextInt()

Page 27: Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.

Reading other types of input from the keyboard (cont.)

• Note: you also need to use an int typed variable to store an integer value !!!