Some Scanner Class Methods

Similar documents
Install Java Development Kit (JDK) 1.8

Lecture 5: Java Fundamentals III

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

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

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

Comp 248 Introduction to Programming

Chapter 3. Input and output. 3.1 The System class

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

6.1. Example: A Tip Calculator 6-1

Chapter 2 Elementary Programming

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

Chapter 2: Elements of Java

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

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

Using Files as Input/Output in Java 5.0 Applications

Introduction to Programming

JavaScript: Introduction to Scripting 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

Introduction to Java. CS 3: Computer Programming in Java

Chapter 8. Living with Java. 8.1 Standard Output

Basics of Java Programming Input and the Scanner class

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

CS 106 Introduction to Computer Science I

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

CS106A, Stanford Handout #38. Strings and Chars

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Moving from CS 61A Scheme to CS 61B Java

Programming Languages CIS 443

Building Java Programs

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

Course Intro Instructor Intro Java Intro, Continued

Introduction to Python

Programmierpraktikum

Object-Oriented Programming in Java

Programming and Data Structures with Java and JUnit. Rick Mercer

Lecture 9. Semantic Analysis Scoping and Symbol Table

Building Java Programs

Problem Solving. Software Engineering. Behavior. OCD in a Nutshell. Behavior. Objects Operations Algorithm. Object-Centered Design

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

Introduction to Java

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

Topic 11 Scanner object, conditional execution

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

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

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

3 Improving the Crab more sophisticated programming

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

Java Crash Course Part I

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

Hypercosm. Studio.

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

CSE 308. Coding Conventions. Reference

PL / SQL Basics. Chapter 3

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

Software Development (CS2500)

Introduction to Python

Variables, Constants, and Data Types

Official Android Coding Style Conventions

1 Using CWEB with Microsoft Visual C++ CWEB INTRODUCTION 1

Arrays in Java. Working with Arrays

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

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

Sample CSE8A midterm Multiple Choice (circle one)

Visit us at

Java Program Coding Standards Programming for Information Technology

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

Week 2 Practical Objects and Turtles

Hands-on Exercise 1: VBA Coding Basics

ECE 341 Coding Standard

JAVA.UTIL.SCANNER CLASS

Chapter 13 - The Preprocessor

JavaScript: Control Statements I

AP Computer Science Static Methods, Strings, User Input

Taxi Service Coding Policy. Version 1.2

6.170 Tutorial 3 - Ruby Basics

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Conditionals (with solutions)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

J a v a Quiz (Unit 3, Test 0 Practice)

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

CS 1133, LAB 2: FUNCTIONS AND TESTING

LOOPS CHAPTER CHAPTER GOALS

Week 1: Review of Java Programming Basics

An Introduction to the WEB Style of Literate Programming

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

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

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

Reading Input From A File

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

CS 241 Data Organization Coding Standards

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

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

Chapter 2 Introduction to Java programming

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

.NET Standard DateTime Format Strings

Software Development Phases

Programming Project 1: Lexical Analyzer (Scanner)

Building Java Programs

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Transcription:

Keyboard Input Scanner, Documentation, Style Java 5.0 has reasonable facilities for handling keyboard input. These facilities are provided by the Scanner class in the java.util package. A package is a library of classes. Using the Scanner Class Near the beginning of your program, insert import java.util.scanner; Create an object of the Scanner class Scanner keyboard = new Scanner (System.in) Read data (an int or a double, for example) int n1 = keyboard.nextint(); double d1 = keyboard,nextdouble(); Some Scanner Class Methods Figure 2.7a 1

Some Scanner Class Methods nextline()method Caution The nextline() method reads The remainder of the current line, Even if it is empty. nextline()method Caution The Empty String Example given following declaration. int n; String s1, s2; n = keyboard.nextint(); s1 = keyboard.nextline(); s2 = keyboard.nextline(); Assume input shown 42 and don't you forget it. n is set to 42 but s1 is set to the empty string. A string can have any number of characters, including zero. The string with zero characters is called the empty string. The empty string is useful and can be created in many ways including String s3 = ""; 2

Other Input Delimiters (optional) Almost any combination of characters and strings can be used to separate keyboard input. to change the delimiter to "##" keyboard2.usedelimiter("##"); whitespace will no longer be a delimiter for keyboard2 input Documentation and Style: Outline Meaningful Names Comments Indentation Named Constants Documentation and Style Meaningful Variable Names Most programs are modified over time to respond to new requirements. Programs which are easy to read and understand are easy to modify. Even if it will be used only once, you have to read it in order to debug it. A variable's name should suggest its use. Observe conventions in choosing names for variables. Use only letters and digits. "Punctuate" using uppercase letters at word boundaries (e.g. taxrate). Start variables with lowercase letters. Start class names with uppercase letters. 3

Comments Comments The best programs are self-documenting. Clean style Well-chosen names Comments are written into a program as needed explain the program. They are useful to the programmer, but they are ignored by the compiler. A comment can begin with //. Everything after these symbols and to the end of the line is treated as a comment and is ignored by the compiler. double radius; //in centimeters Comments Comments A comment can begin with /* and end with */ Everything between these symbols is treated as a comment and is ignored by the compiler. /** This program should only be used on alternate Thursdays, except during leap years, when it should only be used on alternate Tuesdays. */ A javadoc comment, begins with /** and ends with */. It can be extracted automatically from Java software. /** method change requires the number of coins to be nonnegative */ 4

When to Use Comments Comments Example Begin each program file with an explanatory comment What the program does The name of the author Contact information for the author Date of the last modification. Provide only those comments which the expected reader of the program file will need in order to understand it. See Comments.sample.txt Indentation Indentation Indentation should communicate nesting clearly. A good choice is four spaces for each level of indentation. Indentation should be consistent. Indentation should be used for second and subsequent lines of statements which do not fit on a single line. Indentation does not change the behavior of the program. Proper indentation helps communicate to the human reader the nested structures of the program 5

Using Named Constants Named Constants To avoid confusion, always name constants (and variables). area = PI * radius * radius; is clearer than area = 3.14159 * radius * radius; Place constants near the beginning of the program. Once the value of a constant is set (or changed by an editor), it can be used (or reflected) throughout the program. public static final double INTEREST_RATE = 6.65; If a literal (such as 6.65) is used instead, every occurrence must be changed, with the risk than another literal with the same value might be changed unintentionally. Declaring Constants Named Constants Syntax public static final Variable_Type = Constant; Examples public static final double PI = 3.14159; public static final String MOTTO = "The customer is always right."; By convention, uppercase letters are used for constants. see CircleCalculation2 Sample Screen Output 6