COMP String and Console I/O. Yi Hong May 18, 2015

Similar documents
Basic Computation. Chapter 2

COMP Introduction to Programming Primitive Types, Strings and Console I/O

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

COMP 110 Introduction to Programming. What did we discuss?

Basic Computation. Chapter 2

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Basic Computation. Chapter 2

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Getting started with Java

CSE20 : Lab #4 - Data Types

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

Section 2: Introduction to Java. Historical note

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Darrell Bethea May 25, 2011

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Input. Scanner keyboard = new Scanner(System.in); String name;

2: Basics of Java Programming

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

AP Computer Science A

Chapter. Let's explore some other fundamental programming concepts

CS 106 Introduction to Computer Science I

A+ Computer Science -

CMSC131. Introduction to your Introduction to Java. Why Java?

Chapter 2: Data and Expressions

COMP Computer Basics. Yi Hong May 13, 2015

CS 180. Recitation 8 / {30, 31} / 2007

H212 Introduction to Software Systems Honors

CIS 110: Introduction to Computer Programming

We now start exploring some key elements of the Java programming language and ways of performing I/O

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

A variable is a name for a location in memory A variable must be declared

A+ Computer Science -

Chapter 2: Data and Expressions

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Elementary Programming

First Java Program - Output to the Screen

Using Java Classes Fall 2018 Margaret Reid-Miller

Programming with Java

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Object Oriented Programming. Java-Lecture 1

Arithmetic and IO. 25 August 2017

Full file at

Full file at

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Lecture 8 " INPUT " Instructor: Craig Duckett

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

B.V. Patel Institute of BMC & IT, UTU 2014

Lecture 3: Variables and assignment

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

double float char In a method: final typename variablename = expression ;

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Java Basics. SP17 ICS 111 Ed Meyer

Objectives. Primitive Types, Strings, and Console I/O. Variables and Values. Outline. Naming and Declaring Variables. Variables and Values

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Computational Expression

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

4 WORKING WITH DATA TYPES AND OPERATIONS

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Data Conversion & Scanner Class

Intro to Programming in Java Practice Midterm

ECE 122 Engineering Problem Solving with Java

Full file at

Fundamentals of Programming Data Types & Methods

FUNDAMENTAL DATA TYPES

4. Java language basics: Function. Minhaeng Lee

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS 101 Fall 2006 Midterm 1 Name: ID:

Robots. Byron Weber Becker. chapter 6

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Classes and Objects Part 1

5) (4 points) What is the value of the boolean variable equals after the following statement?

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Java Console Input/Output The Basics. CGS 3416 Spring 2018

CMPT 125: Lecture 3 Data and Expressions

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

What did we talk about last time? Examples switch statements

Chapter 2 ELEMENTARY PROGRAMMING

CS111: PROGRAMMING LANGUAGE II

Java Console Input/Output The Basics

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Lecture 10. Instructor: Craig Duckett

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Transcription:

COMP 110-001 String and Console I/O Yi Hong May 18, 2015

Announcements Labs 0 & 1 are due today by 11:59pm Homework 0 grades are posted Office hours are fixed: MW 3-4pm, or by appointment

Review of How to Use a Variable Declare a variable int age; A container to hold data Assign a value to the variable age = 16; Puts a value into the container Change the value of the variable age = age + 1; Interpreted from right to left 3

Review of Last Lecture What are the two main data types in Java? Primitive type and class type List some of the primitive types int, float, double, char, boolean Can a double variable be assigned to an int variable? How to do type casting? (Type_Name) Expression

Today String Console I/O

String A Class Type Objects of String class can be defined as: String mystring = UNC is Great! ; Each String object consists of A sequence of characters (char) String Indices: U N C i s G r e a t! 0 1 2 3 4 5 6 7 8 9 10 11 12 A set of methods that can process the sequence of characters

Some Methods in the Class String String mystring = UNC is Great! U N C i s G r e a t! 0 1 2 3 4 5 6 7 8 9 10 11 12 int strlength = mystring.length(); System.out.println(strLength); char strfirstletter = mystring.charat(0); System.out.println(strFirstLetter); 13 U String strlowercase = mystring.tolowercase(); System.out.println(strLowerCase); unc is great! System.out.println(myString.substring(0, 3)); UNC

String Check Java API for the whole list of methods in String You do not need to memorize them But you should know how to use the classes if you have related documents More methods in the Class String, see p.86 You will learn how to use these methods in Labs on Wednesday

Escape Characters How to put quotes in a string System.out.println( How do I put \ quotes \ in my string? ); But what about backslashes? System.out.println( How do I put \\ in my string? ); \ Double quote \ Single quote \\ Backslash \n New line \r Carriage return \t Tab

More Examples abc\\def abc\def The motto is \ngo for it! The motto is Go for it! How s this How s this char singlequote = \ ;

String Concatenation String name = May ; String sentence; sentence = My dog s name is + name; My dog s name is May

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 str = ;

Try It Yourself Run code in Eclipse See StringsAndChars.java, TestStringMethods.java on the course website for more details

Console I/O: Screen Output and Keyboard Input

Screen Output We ve seen several examples of screen output already System.out.println( This is a string ); System.out is an object built in Java SDK println is one of the methods available to the System.out object

Print Versus Println System.out.print( This is a string ); System.out.println( This is a string ); What is the difference? System.out.print( This prints ); System.out.print( multiple parts ); System.out.println( in one line );

Screen Output Use concatenation operator (+) to join the things you want to display System.out.println("Lucky number = " + 13 + "Secret number = " + number); Note: no spaces are added. If you want to add a space, add it into the string Secret number = à Secret number =

Keyboard Input Java 5.0 ~ 7.0 have 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

How to Use the Scanner Class Include at the beginning of a program import java.util.scanner; Create an object of the Scanner class Scanner Scanner_object_name = new Scanner(System.in); Read data using methods of the object Scanner_object_name.next(); à String Scanner_object_name.nextLine(); à String Scanner_object_name.nextInt(); à int Scanner_object_name.nextDouble(); à double Scanner_object_name.nextBoolean(); à boolean

More About nextline() The nextline() method reads the remainder of the current line, even if it is empty E.g.: int n = keyboard.nextint(); String s1 = keyboard.nextline(); String s2 = keyboard.nextline(); Input: 13 is the lucky number, and which one is the secret number? Make sure to read GOTCHA on p.97

Next Class Review of Chapter 1 & 2 Review programs in lectures and labs