Building Java Programs Chapter 4.3

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Advertisements

Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming File as Input; Exceptions; while loops; Basic Arrays Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
Building Java Programs Chapter 4 Conditional Execution.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
EXAM 1 REVIEW. days until the AP Computer Science test.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Introduction to Programming
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
String and Scanner CS 21a: Introduction to Computing I First Semester,
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CHAPTER 6 GC Strings. THE CLASS STRING  Contains operations to manipulate strings.  String:  Sequence of zero or more characters.  Enclosed.
CS 112 Introduction to Programming String/char; Exceptions; while loops; Basic Arrays Yang (Richard) Yang Computer Science Department Yale University 208A.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, 4.3.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 4 Lecture 4-3: Strings, char reading: 3.3, self-check: Ch. 4 #12, 15 exercises:
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
Building Java Programs
Building Java Programs Chapter 4
Chapter 2 Basic Computation
Building Java Programs
Building Java Programs
Chapter 6 GC 101 Strings.
Adapted from slides by Marty Stepp and Stuart Reges
Conditional Execution
Conditional Execution
Strings string: An object storing a sequence of text characters.
Chapter 2 Basic Computation
Array traversals, text processing
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs
Building Java Programs
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings
Building Java Programs
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Using Objects (continued)
Building Java Programs
Building Java Programs
Building Java Programs
Strings string: An object storing a sequence of text characters.
Building Java Programs
Building Java Programs
Building Java Programs
More on iterations using
Presentation transcript:

Building Java Programs Chapter 4.3 Text processing

Type char char : A primitive type representing single characters. Each character inside a String is stored as a char value. Literal char values are surrounded with apostrophe (single-quote) marks, such as 'a' or '4' or '\n' or '\'' It is legal to have variables, parameters, returns of type char char letter = 'S'; System.out.println(letter); // S char values can be concatenated with strings. char initial = 'P'; System.out.println(initial + " Diddy"); // P Diddy

The charAt method The chars in a String can be accessed using the charAt method. String food = "cookie"; char firstLetter = food.charAt(0); // 'c' System.out.println(firstLetter + " is for " + food); System.out.println("That's good enough for me!"); You can use a for loop to print or examine each character. String major = "CSE"; for (int i = 0; i < major.length(); i++) { char c = major.charAt(i); System.out.println(c); } Output: C S E

char vs. int All char values are assigned numbers internally by the computer, called ASCII values. Examples: 'A' is 65, 'B' is 66, ' ' is 32 'a' is 97, 'b' is 98, '*' is 42 Mixing char and int causes automatic conversion to int. 'a' + 10 is 107, 'A' + 'A' is 130 To convert an int into the equivalent char, type-cast it. (char) ('a' + 2) is 'c'

char vs. String "h" is a String 'h' is a char (the two behave differently) String is an object; it contains methods String s = "h"; s = s.toUpperCase(); // 'H' int len = s.length(); // 1 char first = s.charAt(0); // 'H' 5

char vs. String char is primitive; you can't call methods on it char c = ‘a'; c = c.toUpperCase(); // ERROR: "cannot be dereferenced" String s = “a"; What is s + 1 ? What is c + 1 ? What is s + s ? What is c + c ? C+1 = 98 C+c = 194 S+1=a1 S+s=aa 6

Comparing char values You can compare char values with relational operators: 'a' < 'b' and 'X' == 'X' and 'Q' != 'q' An example that prints the alphabet: for (char c = 'a'; c <= 'z'; c++) { System.out.print(c); }

Comparing char values You can test the value of a string's character: String word = console.next(); if (word.charAt(word.length() - 1) == 's') { System.out.println(word + " is plural."); }

String/char question A Caesar cipher is a simple encryption where a message is encoded by shifting each letter by a given amount. e.g. with a shift of 3, A  D, H  K, X  A, and Z  C Write a program that reads a message from the user and performs a Caesar cipher on its letters: Your secret message: Brad thinks Angelina is cute Your secret key: 3 The encoded message: eudg wklqnv dqjholqd lv fxwh

Strings answer 1 // This program reads a message and a secret key from the user and // encrypts the message using a Caesar cipher, shifting each letter. import java.util.*; public class SecretMessage { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Your secret message: "); String message = console.nextLine(); message = message.toLowerCase(); System.out.print("Your secret key: "); int key = console.nextInt(); encode(message, key); } ...

Strings answer 2 // This method encodes the given text string using a Caesar // cipher, shifting each letter by the given number of places. public static void encode(String text, int shift) { System.out.print("The encoded message: "); for (int i = 0; i < text.length(); i++) { char letter = text.charAt(i); // shift only letters (leave other characters alone) if (letter >= 'a' && letter <= 'z') { letter = (char) (letter + shift); // may need to wrap around if (letter > 'z') { letter = (char) (letter - 26); } else if (letter < 'a') { letter = (char) (letter + 26); } System.out.print(letter); System.out.println();