© A+ Computer Science - www.apluscompsci.com. Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
18 File handling1June File handling CE : Fundamental Programming Techniques.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
© 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.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
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.
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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Expressions Methods if else Statements Loops Potpourri.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
AP Java Java’s version of Repeat until.
© 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
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Lecture 4 – Scanner & Style
4. Java language basics: Function
Input/Output.
Chapter 2 More on Math More on Input
Computer Programming Methodology Input and While Loop
Peer Instruction 9 File Input/Output.
Chapter 5: Control Structures II
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Program Style Console Input and Output
Lesson A4 – Object Behavior
CSS 161 Fundamentals of Computing Introduction to Computers & Java
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
A+ Computer Science INPUT.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Control Statements Loops.
CS150 Introduction to Computer Science 1
© A+ Computer Science - Classes And Objects © A+ Computer Science -
A+ Computer Science INPUT.
See requirements for practice program on next slide.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Control Statements Loops.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Math class services (functions)
CSS161: Fundamentals of Computing
Computer Science Club 1st November 2019.
Presentation transcript:

© A+ Computer Science -

Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next double value next()returns the next one word String nextLine()returns the next multi word String hasNextInt()checks to see if there are more ints hasNextDouble()checks to see if there are more doubles hasNext()checks to see if there are more Strings import java.util.Scanner;

© A+ Computer Science - int num = keyboard.nextInt(); reference variable method call

© A+ Computer Science - out.print("Enter a string :: "); String word = keyboard.next(); out.println(word); OUTPUT Enter a string :: I love java. I INPUT I love java.

© A+ Computer Science - out.print("Enter a line :: "); String line = keyboard.nextLine(); out.println(line); OUTPUT Enter a line :: I love java. I love java. INPUT I love java.

© A+ Computer Science - out.print("Enter an integer :: "); int num = keyboard.nextInt(); out.print("Enter a sentence :: "); String sentence = keyboard.nextLine(); out.println(num + " "+sentence); OUTPUT Enter an integer :: 34 Enter a sentence :: 34 INPUT 34 picks up \n nextLine() picks up whitespace.

© A+ Computer Science - 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); OUTPUT Enter an integer :: 34 Enter a sentence :: picks up \n 34 picks up \n INPUT 34 picks up \n nextLine() picks up whitespace.

© A+ Computer Science - Scanner keyboard = new Scanner(System.in); out.println(keyboard.nextInt()); OUTPUT INPUT

© A+ Computer Science -

Scanner keyboard = new Scanner(System.in); Scanner chopper = new Scanner("at it us"); object / referenceconstructor

© A+ Computer Science - Scanner chopper = new Scanner(" "); out.println(chopper.nextInt()); OUTPUT

© A+ Computer Science - Scanner chopper = new Scanner("one two fun"); out.println(chopper.next()); OUTPUT one two fun

© A+ Computer Science - Scanner chopper = new Scanner("one two fun"); out.println(chopper.next()); OUTPUT one two fun error

© A+ Computer Science -

Scanner frequently used methods NameUse hasNextByte()checks to see if there are more bytes hasNextShort()checks to see if there are more shorts hasNextInt()checks to see if there are more ints hasNextLong()checks to see if there are more longs hasNextDouble()checks to see if there are more doubles hasNext()checks to see if there are more Strings All of these methods return true or false.

© A+ Computer Science - while ( I have candy ) { } DIAGNOSIS Infinite Loop! No candy was eaten.

© A+ Computer Science - while ( I have candy ) { eat a piece of candy } DIAGNOSIS All candy gets eaten.

© A+ Computer Science - String input = " "; Scanner chopper = new Scanner(input); while (chopper.hasNextInt()) { out.println(chopper.nextInt()); } OUTPUT DIAGNOSIS All candy gets eaten.

© A+ Computer Science - out.print("Enter a list of integers :: "); String input = kb.nextLine(); Scanner chopper = new Scanner(input); while (chopper.hasNextInt()) { out.println(chopper.nextInt()); } OUTPUT # # #... This setup is required when the item count is unknown.

© A+ Computer Science - out.print("Enter a sentence :: "); String line = kb.nextLine(); Scanner chopper = new Scanner(line); while (chopper.hasNext()) { out.println(chopper.next()); } This setup is required when the item count is unknown.

© A+ Computer Science -

useDelimiter() //specifies split value

© A+ Computer Science - Scanner chopper = new Scanner("one-two-three"); chopper.useDelimiter("\\-"); while(chopper.hasNext()) { out.println(chopper.next()); } OUTPUT one two three

© A+ Computer Science -

Scanner keyboard = new Scanner(System.in); out.print("How many numbers ::"); int count = keyboard.nextInt(); int sum = 0; for(int i=0;i<count;i++) { out.print("Enter number " + (i+1) + " :: "); sum=sum+keyboard.nextInt(); } out.println("total == " + sum);

© A+ Computer Science -