Basics of Java Programming Input and the Scanner class

Similar documents
Interactive Programs and Graphics in Java

Topic 11 Scanner object, conditional execution

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Building Java Programs

Building Java Programs

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Install Java Development Kit (JDK) 1.8

Introduction to Java

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Introduction to Java. CS 3: Computer Programming in Java

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

AP Computer Science Static Methods, Strings, User Input

Comp 248 Introduction to Programming

Building Java Programs

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Visit us at

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Introduction to Programming

Chapter 2: Elements of Java

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

6.1. Example: A Tip Calculator 6-1

Object-Oriented Programming in Java

CS 106 Introduction to Computer Science I

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

JAVA.UTIL.SCANNER CLASS

Chapter 3. Input and output. 3.1 The System class

Sample CSE8A midterm Multiple Choice (circle one)

Building Java Programs

Some Scanner Class Methods

Using Files as Input/Output in Java 5.0 Applications

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Programming Fundamentals I CS 110, Central Washington University. November 2015

JAVA ARRAY EXAMPLE PDF

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Line-based file processing

Data Structures Lecture 1

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

The following program is aiming to extract from a simple text file an analysis of the content such as:

Chapter 2 Elementary Programming

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Chapter 2 Introduction to Java programming

13 File Output and Input

Week 1: Review of Java Programming Basics

AP Computer Science File Input with Scanner

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

More on Objects and Classes

Lab 5: Bank Account. Defining objects & classes

Reading Input From A File

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

AP Computer Science Java Mr. Clausen Program 9A, 9B

Using Two-Dimensional Arrays

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

IMDB Data Set Topics: Parsing Input using Scanner class. Atul Prakash

Interactive Applications (CLI) and Math

Programming in Java Course Technology, a part of Cengage Learning.

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Event-Driven Programming

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CSE 8B Midterm Fall 2015

Part I. Multiple Choice Questions (2 points each):

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Programming and Data Structures with Java and JUnit. Rick Mercer

(Eng. Hayam Reda Seireg) Sheet Java

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Object-Oriented Programming

Word Count Code using MR2 Classes and API

Programming in Java. What is in This Chapter? Chapter 1

Course Intro Instructor Intro Java Intro, Continued

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Arrays in Java. Working with Arrays

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Chapter 2 Basics of Scanning and Conventional Programming in Java

In this Chapter you ll learn:

D06 PROGRAMMING with JAVA

Programmierpraktikum

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

java Features Version April 19, 2013 by Thorsten Kracht

Chapter 8. Living with Java. 8.1 Standard Output

Lecture 1 Introduction to Java

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Assignment 4 Solutions

Chapter 5. Recursion. Data Structures and Algorithms in Java

Lecture 5: Java Fundamentals III

Transcription:

Basics of Java Programming Input and the Scanner class CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying: Java Software Solutions by Lewis & Loftus Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne

Review Variables A variable is a name for a location in memory, of a declared type An assignment statement associates a value with a variable A variable can be given an initial value in the declaration int age; double x = 3.2, y = -0.80; age = 18; x = 3.3; String name = scan.nextline();

Variables A variable is a name for a location in memory, of a declared type An assignment statement associates a value with a variable A variable can be given an initial value in the declaration Next: assigning a value obtained while the program is running int age; double x = 3.2, y = -0.80; age = 18; x = 3.3; String name = scan.nextline();

Interactive Programs Input/Output Programs can use data obtained during runtime, eg: int age; String name; Scanner scan = new Scanner(System.in);! output method System.out.print( Enter your name ); name = scan.nextline(); input method System.out.print( Enter your age ); age = scan.nextint(); );

Interactive Programs Input/Output In Java, you first need to create a Scanner object int age; String name; Scanner scan = new Scanner(System.in);! System.out.print( Enter your name ); name = scan.nextline(); System.out.print( Enter your age ); age = scan.nextint(); Scanner object input method (for String) input method (for int)

Reading Input The Scanner class is part of the java.util class library, and must be imported into a program in order to be used The import statement goes at beginning of your program (above class definition) import java.util.scanner;

Using the Scanner class 1. import the class, i.e., add this before the class definition of your program: import java.util.scanner; 2. In your main method, before doing any input, declare and initialize the Scanner object Scanner scan = new Scanner(System.in); 3. Input away! System.out.print( Enter your name ); name = scan.nextline(); System.out.print( Enter your age ); age = scan.nextint();string

Using the Scanner class import java.util.scanner; public class TellMeAboutYou { public static void main(string[] args) { int age; String name; Scanner scan = new Scanner(System.in); System.out.print(Enter your name); name = scan.nextline(); System.out.print(Enter your age); age = scan.nextint(); System.out.println(Pleased to meet you, + name +!); System.out.println(Your age in dog years: + age*10.5); } } name = scan.nextline(); Enter your name: Fiona Enter your age: 17 Pleased to meet you, Fiona! Your age in dog years is 178.5 Inspired by: http://www.onlineconversion.com/dogyears.htm

Input methods nextint() à input an int nextdouble() à input a double nextline() à input a String (until end of line) next() à input a String token (one word or other delimited chunk of text) White space (space, tab, new line) are used to separate input tokens

Using the Scanner class import java.util.scanner; public class TellMeAboutYou { public static void main(string[] args) { Enter your name and age: Fiona 17 Pleased to meet you, Fiona! Your age in dog years is 178.5 int age; String name; Scanner scan = new Scanner(System.in); fill in missing code System.out.print(Enter your name and age: ); name = scan.nextline(); age = scan.nextint(); System.out.println(Pleased to meet you, + name +!); System.out.println(Your age in dog years: + age*10.5); } } name = scan.nextline(); More examples see text: Echo.java GasMileage.java