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.

Slides:



Advertisements
Similar presentations
Importing methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Shorthand operators.
The character data type char
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Mixing integer and floating point numbers in an arithmetic operation.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
CompSci 230 S Programming Techniques
TemperatureConversion
Data types, Expressions and assignment, Input from User
Comp Sci 200 Programming I Jim Williams, PhD.
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Computers & Programming Languages
The Boolean (logical) data type boolean
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Consider the following code:
Presentation transcript:

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 window where you type in the command java ProgramName

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

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

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

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

Java's Scanner library functions (cont.) Webpage of the Java documentation on Scanner class: nner.html nner.html

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

Importing the Scanner class definition Recall the Rule of usage of methods in the Java library: (See: /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)

Importing the Scanner class definition (cont.) We can use the following import clause to import the Scanner class: import java.util.Scanner;

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 you how to do it

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);

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()

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:

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 !!!

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

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

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); }

Reading in a floating point number from the keyboard (cont.) Example Program: (Demo above code) –Prog file: 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

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.

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

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

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); }

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:

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.

Reading other types of input from the keyboard (cont.) Reading an integer number from the keyboard: use nextInt()

Reading other types of input from the keyboard (cont.) Note: you also need to use an int typed variable to store an integer value !!!