Lecture 4 – Scanner & Style

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
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.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Operators.
Java Fundamentals Part Topics –Simple java program –The print and println Methods –Java API –Variables and Literals –Primitive Data Types –Arithmetic.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSC 110 – Intro to Computing - Programming
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
CompSci 230 S Programming Techniques
C++ First Steps.
Lecture 2a- Java Fundamentals
Chapter 6 JavaScript: Introduction to Scripting
Input/Output.
Chapter 2: Java Fundamentals by Tony Gaddis and Godfrey Muganda
Chapter 2: Java Fundamentals by Tony Gaddis and Godfrey Muganda
Console Output, Variables, Literals, and Introduction to Type
Java Programming: From Problem Analysis to Program Design, 4e
Scope, Comments, Code Style, Keyboard Input
Primitive Types Operators Basic input and output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 2: Java Fundamentals
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
A+ Computer Science INPUT.
Chapter 2: Java Fundamentals
MSIS 655 Advanced Business Applications Programming
A+ Computer Science INPUT.
Java Programming First Program and Variables
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Instructor: Alexander Stoytchev
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Lecture 4 – Scanner & Style

Console Output The console that starts a Java application is typically known as the standard output device. The standard input device is typically the keyboard. Java sends information to the standard output device by using a Java class stored in the standard Java library.

Console Output Java classes in the standard Java library are accessed using the Java Applications Programming Interface (API). The standard Java library is commonly referred to as the Java API.

Console Output This example uses the line: System.out.println("Programming is great fun!"); This line uses the System class from the standard Java library. The System class contains methods and objects that perform system level tasks. The out object, a member of the System class, contains the methods print and println.

Console Output The println method places a newline character at the end of whatever is being printed out. The following lines: System.out.println(“This is being printed out"); System.out.println(“on two separate lines."); Would be printed out on separate lines since the first statement sends a newline command to the screen.

Java Escape Sequences \n newline Advances the cursor to the next line for subsequent printing \t tab Causes the cursor to skip over to the next tab stop \b backspace Causes the cursor to back up, or move left, one position \r carriage return Causes the cursor to go to the beginning of the current line, not the next line \\ backslash Causes a backslash to be printed \’ single quote Causes a single quotation mark to be printed \” double quote Causes a double quotation mark to be printed

Java Escape Sequences Even though the escape sequences are comprised of two characters, they are treated by the compiler as a single character. System.out.print("These are our top sellers:\n"); System.out.print("\tComputer games\n\tCoffee\n "); System.out.println("\tAspirin"); Would result in the following output: These are our top seller: Computer games Coffee Asprin With these escape sequences, complex text output can be achieved.

The + Operator The + operator can be used in two ways. as a concatenation operator as an addition operator If either side of the + operator is a String, the result will be a String. System.out.println(“Hello “ + “World”); System.out.println(“The value is: “ + 5); System.out.println(“The value is: “ + value); System.out.println(“The value is: “ + ‘/n’ + 5);

Arithmetic Operators Java has five (5) binary arithmetic operators. Meaning Type Example + Addition Binary total = cost + tax; - Subtraction cost = total – tax; * Multiplication tax = cost * rate; / Division salePrice = original / 2; % Modulus remainder = value % 5;

Creating Constants Many programs have data that does not need to be changed. Littering programs with literal values can make the program hard to read and maintain. replacing literal values with constants remedies this problem. Constants allow the programmer to use a name rather than a value throughout the program. Constants also give a singular point for changing those values when needed.

Creating Constants Constants keep the program organized and easier to maintain. Constants are identifiers that can hold only a single value. Constants are declared using the keyword final. Java constants need not be initialized when declared; however, they must be initialized before they are used or a compiler error will be generated.

Creating Constants Once initialized with a value, constants cannot be changed programmatically. By convention, constants are all upper case and words are separated by the underscore character. final int CAL_SALES_TAX = 0.725;

Programming Style Although Java has a strict syntax, whitespace characters are ignored by the compiler. The Java whitespace characters are: space tab newline carriage return form feed Compact.java

Indentation Programs should use proper indentation. Each block of code should be indented a few spaces from its surrounding block. Two to four spaces are sufficient. Tab characters should be avoided. Tabs can vary in size between applications and devices. Most programming text editors allow the user to replace the tab with spaces.

The Scanner Class Java was designed primarily to receive input from a graphical user interface (GUI). Getting information from the keyboard in a console application is not convenient. We can use the Scanner class to simplify standard input

The Scanner Class (Cont) The Scanner class is defined in java.util, so we import java.util.Scanner; Scanner objects work with System.in To create a Scanner object, Scanner keyboard ; keyboard = new Scanner (System.in); Scanner class methods are listed in Table 2-18 in the text.

SCANNER EXAMPLE 1 import java.util.Scanner; public class NoProblem { public static void main(String[] args) { int studentNumber; int age; double income; Scanner keyboard; keyboard = new Scanner(System.in); System.out.print("What is your age? "); age = keyboard.nextInt(); System.out.print("What is your annual income? "); income = keyboard.nextDouble(); System.out.print(“What is your student number? "); studentNumber = keyboard.nextInt(); System.out.println ("Hello " + studentNumber + ". Your age is " + age + " and your income is $" + income); } }

SCANNER EXAMPLE 2 import java.util.Scanner; public class InputProblem { public static void main(String[] args) { String name; int age; double income; Scanner keyboard keyboard = new Scanner(System.in); System.out.print("What is your age? "); age = keyboard.nextInt(); System.out.print("What is your annual income? "); income = keyboard.nextDouble(); System.out.print("What is your name? "); name = keyboard.nextLine(); System.out.println ("Hello " + name + ". Your age is " + age + " and your income is $" + income); } }

SCANNER EXAMPLE 3 import java.util.Scanner; public class CorrectedInputProblem { public static void main(String[] args) { String name; int age; double income; Scanner keyboard keyboard = new Scanner(System.in); System.out.print("What is your age? "); age = keyboard.nextInt(); System.out.print("What is your annual income? "); income = keyboard.nextDouble(); keyboard.nextLine(); System.out.print("What is your name? "); name = keyboard.nextLine(); System.out.println ("Hello " + name + ". Your age is " + age + " and your income is $" + income); } }

Understanding Scanner User’s keystrokes are stored in an area of memory called the keyboard buffer (generally referred to as the buffer) Pressing enter causes a newline character to be stored in the buffer. nextInt will stop when it encounters the newline which remains in the buffer nextInt, nextDouble, etc. will skip any leading newline characters nextLine will not (the newline is a character). nextLine will therefore pick up the newline as its input and the user won’t be able to enter anything. Fix the problem by inserting a keyboard.nextLine(); statement and not storing the newline character it retrieves.