A+ Computer Science INPUT.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Chapter 2: Java Fundamentals Input and Output statements.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 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.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
Expressions, Data Conversion, and Input
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
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.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
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.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
© A+ Computer Science - Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Building Java Programs
String Comparison, Scanner
Input/Output.
Chapter 2 More on Math More on Input
Console Input and Output
TemperatureConversion
Chapter 3 Java Input/Output.
Console Input and Output
INPUT STATEMENTS GC 201.
Scope, Comments, Code Style, Keyboard Input
Program Style Console Input and Output
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Chapter 3 Input/Output.
Building Java Programs
Chapter 2: Java Fundamentals
© A+ Computer Science - Classes And Objects © A+ Computer Science -
A+ Computer Science INPUT.
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Math class services (functions)
Input, Variables, and Mathematical Expressions
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

A+ Computer Science INPUT

© A+ Computer Science - www.apluscompsci.com Imports © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Scanner Import import java.util.Scanner; Try to be as specific as possible when using an import. In order to use Scanner, you must import java.util.Scanner. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Scanner Creation reference variable Scanner keyboard = new Scanner(System.in); Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created. System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location/memory address of newly created Scanner object. object instantiation © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Scanner Methods © A+ Computer Science - www.apluscompsci.com

frequently used methods Scanner frequently used methods Name Use nextInt() returns the next int value nextDouble() returns the next double value nextFloat() returns the next float value nextLong() returns the next long value nextByte() returns the next byte value nextShort() returns the next short value next() returns the next one word String nextLine() returns the next multi word String This chart lists the Scanner methods that will be used most frequently. More Scanner methods will be introduced later. import java.util.Scanner; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Integer Input © A+ Computer Science - www.apluscompsci.com

Reading Integer Values Scanner keyboard = new Scanner(System.in); out.print("Enter an integer :: "); int num = keyboard.nextInt(); The nextInt() method is used to tell a Scanner object to retrieve the next integer value entered. In the example, the next integer typed in on the keyboard would be read in and placed in the integer variable num. nextInt() will read up to the first whitespace value entered. Whitespace would be any enter(\n), tab(\t), or space. © A+ Computer Science - www.apluscompsci.com

Reading Integer Values out.print("Enter an integer :: "); int num = keyboard.nextInt(); out.println(num); INPUT 2001 OUTPUT Enter an integer :: 2001 2001 The nextInt() method is used to tell a Scanner object to retrieve the next integer value entered. In the example, the next integer typed in on the keyboard would be read in and placed in the integer variable num. nextInt() will read up to the first whitespace value entered. © A+ Computer Science - www.apluscompsci.com

Reading Integer Values reference variable int num = keyboard.nextInt(); The nextInt() method will read in the next integer. If a non-integer value is encountered such as a decimal value, the result will be run-time exception. keyboard is a reference that refers to a Scanner object. method call © A+ Computer Science - www.apluscompsci.com

Reading Integer Values System.out.print("Enter an integer :: "); Prompts are used to tell the user what you want. When performing input operations, it is a must to use prompts. A prompt is a way of indicating to a user what type of data to enter. The prompt above indicates that an integer value is expected. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com int_input.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Double Input © A+ Computer Science - www.apluscompsci.com

Reading Decimal Values Scanner keyboard = new Scanner(System.in); out.print("Enter a double :: "); double num = keyboard.nextDouble(); The nextDouble() method will read in the next numeric value entered. Any integer or decimal value will be accepted. In the example, the next numeric value entered on the keyboard would be read in and placed in variable num. nextDouble() will read up to the first whitespace value entered. © A+ Computer Science - www.apluscompsci.com

Reading Decimal Values reference variable double num = keyboard.nextDouble(); The nextDouble() method will read in the next numeric value. If a non-numeric value is encountered such as a text value or word, the result will be run-time exception. keyboard is a reference that refers to a Scanner object. method call © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com double_input.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com String Input © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Reading String Values Scanner keyboard = new Scanner(System.in); out.print("Enter a string :: "); String word = keyboard.next(); The next() method will read in the next text value entered. A numeric or non-numeric text value will be accepted. In the example, the next text entered on the keyboard would be read in and placed in variable word. The next() method would read up to the first whitespace encountered. Whitespace would be any space, any tab, or any enter key. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Reading String Values out.print("Enter a string :: "); String word = keyboard.next(); out.println(word); INPUT I love A+ compsci. OUTPUT Enter a string :: I love A+ compsci. I The next() method will read in the next text value entered. A numeric or non-numeric text value will be accepted. In the example, the next text entered on the keyboard would be read in and placed in variable word. The next() method would read up to the first whitespace encountered. Whitespace would be any space, any tab, or any enter key. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Reading String Values Scanner keyboard = new Scanner(System.in); out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); The nextLine() method will read in an entire line of text including whitespace(enter keys, spaces, tabs, etc.). Any text value entered will be accepted, including a line containing spaces. In the example, the next line of data entered on the keyboard would be read in and placed in variable sentence. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Reading String Values out.print("Enter a line :: "); String line = keyboard.nextLine(); out.println(line); INPUT I love A+ compsci. OUTPUT Enter a line :: I love A+ compsci. I love A+ compsci. The nextLine() method will read in an entire line of text including whitespace(enter keys, spaces, tabs, etc.). Any text value entered will be accepted, including a line containing spaces. In the example, the next line of data entered on the keyboard would be read in and placed in variable sentence. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com string_input.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Input Issues © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Input Issues out.print("Enter an integer :: "); int num = keyboard.nextInt(); out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); out.println(num + " "+sentence); INPUT 34 picks up \n OUTPUT Enter an integer :: 34 Enter a sentence :: 34 The nextLine() method will read in an entire line of text including the enter key. Any text value entered will be accepted, including a line containing spaces. After 34 is typed in, enter must be pressed to get the system to register the 34. nextInt() reads in the 34 and stores it in num. nextInt() reads up to the enter key(\n) typed in after the 34. nextLine() reads in the enter(\n) and stores it in sentence. This is a problem. nextLine() picks up whitespace. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Input Issues out.print("Enter an integer :: "); int num = keyboard.nextInt(); keyboard.nextLine(); //pick up whitespace out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); out.println(num + " "+sentence); INPUT 34 picks up \n OUTPUT Enter an integer :: 34 Enter a sentence :: picks up \n 34 picks up \n The nextLine() method will read in an entire line of text including the enter key. Any text value entered will be accepted, including a line containing spaces. After 34 is typed in, enter must be pressed to get the system to register the 34. nextInt() reads in the 34 and stores it in num. nextInt() reads up to the enter key(\n) typed in after the 34. A nextLine() is placed after the nextInt() to read in the enter(\n). The additional nextLine() picks up the enter(\n) left behind by nextInt(); Now, nextLine() can read in the line and store it in sentence. The problem has been solved. nextLine() picks up whitespace. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com input_issues.java © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Multi Value Input © A+ Computer Science - www.apluscompsci.com

Reading Multiple Values INPUT 7 5 3 1 8 Scanner keyboard = new Scanner(System.in); out.println(keyboard.nextInt()); OUTPUT 7 5 3 Scanner can be used to read in multiple values on one line as long as whitespace is entered in between each value on the line. If whitespace is not used to separate the values, the values would be considered one value. For the example,if 1 2 3 4 5 is entered. Only values 1 2 3 are read in because the code only had 3 nextInt() method calls. If 12345, was entered with no spaces, then 12345 would be the first and only value read in. © A+ Computer Science - www.apluscompsci.com

Reading Multiple Values INPUT 7 5 3 1 8 Scanner keyboard = new Scanner(System.in); out.println( keyboard.nextInt() + keyboard.nextInt() ); OUTPUT 12 Scanner can be used to read in multiple values on one line as long as whitespace is entered in between each value on the line. If whitespace is not used to separate the values, the values would be considered one value. For the example,if 1 2 3 4 5 is entered as input for the code above, the code would print 3. 1 + 2 are read in and their sum is printed on the screen. The code above prints the sum of the first two int values typed in. © A+ Computer Science - www.apluscompsci.com

Reading Multiple Values INPUT 3 9-4 1 8 Scanner keyboard = new Scanner(System.in); int sum = keyboard.nextInt(); sum = sum + keyboard.nextInt(); System.out.println( sum ); OUTPUT 8 Scanner can be used to read in multiple values on one line as long as whitespace is entered in between each value on the line. If whitespace is not used to separate the values, the values would be considered one value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com multi_input.java © A+ Computer Science - www.apluscompsci.com

Work on Programs! Crank Some Code! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com INPUT © A+ Computer Science - www.apluscompsci.com