Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Lecture 6 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Introduction to Objects and Input/Output
Chapter 2: Using Data.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
03 Data types1June Data types CE : Fundamental Programming Techniques.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 2: Java Fundamentals Input and Output statements.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 62 – Manipulating Data Using Methods – Day 1.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2: Using Data.
COMP String and Console I/O Yi Hong May 18, 2015.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,
Chapter 2 print / println String Literals Escape Characters Variables / data types.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Lecture 4 – Scanner & Style
What Actions Do We Have Part 1
Program: Shape Area Print the area for various shape types using the respective formulas.
String class.
Chapter 3 Java Input/Output.
Examples of Primitive Values
Introduction to Classes and Methods
Chapter 2 - Introduction to Java Applications
Introduction to Computer Science and Object-Oriented Programming
Chapter 2 Part 2 Edited by JJ Shepherd
CMSC 202 Java Primer 2.
Wednesday 09/23/13.
Chapter 3 Input/Output.
Introduction to Java Programming
Chapter 2: Java Fundamentals
More on iterations using
Presentation transcript:

Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming

SE15: 5–25–2 Today’s Learning Objectives Learn about the Java data type used for strings of characters Learn how to do simple string manipulation Learn how to do window-based input and output

SE15: 5–35–3 Lecture Outline The Class String Concatenation of Strings String Methods Scanner Class revisited JOptionPane

SE15: 5–45–4 The Class String The class called String can be used to store and process strings of characters. A value of type String is a sequence of characters treated as a single item. You have already used constants of type String System.out.println( “Enter a whole number”); You can also declare variables of type String String greeting; greeting = “Hi, how are you?”;

SE15: 5–55–5 Concatenation of Strings You can connect two strings together using the + operator This is known as concatenation String greeting = “Hello”; String sentence; Sentence = greeting + “Andy”; System.out.println(sentence); will write HelloAndy

SE15: 5–65–6 Classes A class is a type whose values are objects. Objects are entities that store data and take actions Objects of type String store data consisting of a sequence of characters The actions that an object can take are called methods Most methods of the string class return or produce some value e.g. the method length() returns the number of characters in a String object String greeting = “Hello”; int n = greeting.length();

SE15: 5–75–7 String Methods The String methods can be used to manipulate string values length() toUpperCase() Many of the methods depend on counting positions in the string indexOf(“is”) returns 5, the index of the substring “is” charAt(8) returns ‘f’ SE15isfun Savitch 80-82

SE15: 5–85–8 Escape Characters How do you include quotation marks in a String? \”Double quote \’Single Quote \\Backslash \nNew line (go to start of the next line) \rCarriage return (go to start of current line) \tTab

SE15: 5–95–9 Scanner Methods (The actions that Scanner can perform) nextInt() reads one int value typed in at the keyboard and returns its value Others exist for each other primitive type, such as: nextDouble() nextBoolean() To handle strings next() returns the String value consisting of the next keyboard characters up to, but including the first delimiter. nextln() returns rest of the keyboard line (the \n is discarded)

SE15: 5–10 JOptionPane JOptionPane provides a simple window interface to interact with users showInputDialog returns a String entered by the user showMessageDialog displays a String in a window String enteredString; enteredString = JOptionPane.showInputDialog( “Enter a word”); JOptionPane.showMessageDialog(null,”Hello”); Savitch

SE15: 5–11 Summary Looked at the String Class and how strings of characters may be manipulated using the methods of the class Reviewed the use of the Scanner Class Met the JOptionPane for simple window I/O

SE15: 5–12 Follow-up Work Exercise 3 Savitch chapter 2 Rewrite the change calculating program to use the JOptionPane and to allow uses to enter values in pounds and pence. In addition to the existing output, the program should output the numbers of £50, £20, £10, and £5 notes required and £1 and £2 coins.