1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
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.
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!");
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Methods & Activation Record. Recap: what’s a method?
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
From BlueJ to NetBeans SWC 2.semester.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
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.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
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.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Classes - Intermediate
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
(Dreaded) Quiz 2 Next Monday.
Building Java Programs
Strings CSE 1310 – Introduction to Computers and Programming
Chapter 2 Clarifications
Lecture 14 Throwing Custom Exceptions
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 2 Elementary Programming
Computer Programming Methodology Input and While Loop
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
Introduction to Classes and Methods
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
CSC1401 Input and Output (with Files)
Building Java Programs
More on iterations using
Optional Topic: User Input with Scanner
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT

2 LET’S GET INTERACTIVE! We’ve been limited to writing programs that are completely self-contained… everything they do is based on information you provided when writing the program. It’s time to learn how to make our programs interactive, so they can ask for input at runtime! We’ll do this using an instance of the Scanner class.

3 SCANNER For most objects (including Scanner objects), we create a new instance with the new keyword: TypeName myInstance = new TypeName(any, parameters); For a Scanner, it looks like this: Scanner scanner = new Scanner(System.in);

4 A BIT MORE MAGIC: IMPORT There’s one other thing we have to do before we can start using our Scanner. We have to tell Java where it can find it! We do this with one more magic Java keyword, import, at the top of the Java source code file: import java.util.*; Eclipse will offer to do this for us if we mouse over the word “ Scanner ” when it has a red squiggly.

5 SCANNER We finally have enough to start using the methods on our Scanner object: import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); }

6 SCANNER What will this output? I don’t know! It depends on what you type at runtime! import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); }

7 LET’S TRY IT! What is the radius? 3 A circle with radius 3.0 has circumference A circle with radius 3.0 has area A sphere with radius 3.0 has volume

8 IN YOUR NOTEBOOK The following method signatures are missing their class. List the class to which each method belongs. double abs(double); double nextDouble(); char charAt(int); int ceil(double); int length(); String substring(int, int);