Strings CIS 362. What is a string? In Visual Basic fullName.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

56 th AGM * “Future Vision.....Forward Movement” Your Name Here Your Title Here Your Company Here Add speech title.
DTDs : definitions. Defining Elements PCDATA: Parsed character data i.e., any characters without further XML structure.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Some basic I/O.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Fundamental Programming Structures in Java: Strings.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
TA: Nouf Al-Harbi NoufNaief.net :::
REMEMBER: A number can be negative or positive as shown in the number line below: Negative Numbers (-) Positive Numbers (+)
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Computer Programming Lab(4).
HOW TO ADD INTEGERS REMEMBER:
Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Java 1.5 The New Java Mike Orsega Central Carolina CC.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Arrays and Objects CIS 362. The familiar Employee class.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
By Mr. Muhammad Pervez Akhtar
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
CLICK THE NUMBERS IN SEQUENCE
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
The need for Programming Languages
Strings CSE 1310 – Introduction to Computers and Programming
using System; namespace Demo01 { class Program
PowerPoint Presentation Authors of Exposure Java
Syntax & Semantics UML - Java
Chapter 5: Control Structures II
An Introduction to Java – Part I
Control Statement Examples
Introduction to Java Programming
String Methods: length substring
Code Animation Examples
The Basics of Recursion
class PrintOnetoTen { public static void main(String args[]) {
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
EnglishZero.com Free English Lessons… Zero Cost Zero Limits
CLICK THE NUMBERS IN SEQUENCE
In this class, we will cover:
Dr. Sampath Jayarathna Cal Poly Pomona
Just Enough Java 17-May-19.
CLICK THE NUMBERS IN SEQUENCE
More on iterations using
Presentation transcript:

Strings CIS 362

What is a string? In Visual Basic fullName

In C++

In Java

public class StringTest1 { public static void main(String[] args) { String fullName = new String("John Smith"); System.out.println(fullName.toLowerCase()); System.out.println(fullName.toUpperCase()); System.out.println(fullName.charAt(5)); System.out.println(fullName.substring(5)); System.out.println(fullName.substring(3,6)); System.out.println(fullName.length()); }

public class StringTest2 { public static void main(String[] args) { String fullName = " John Smith "; System.out.println(fullName.trim().length()); System.out.println(fullName.length()); fullName = fullName.trim(); System.out.println(fullName.length()); int x = fullName.indexOf("S"); System.out.println(fullName.substring(x)); x = fullName.lastIndexOf("h"); System.out.println(fullName.substring(x)); }

public class StringTest3 { public static void main(String[] args) { String fullName = GetData.getString("Enter the name"); if (fullName.equals(“Joe")) { System.out.println("Hey - it's Joe Spludnik."); } else { System.out.println("Hello " + fullName); } Enter the name: Bill Hello Bill Enter the name: Joe Hey - it's Joe Spludnik Enter the name: JOE Hello JOE

public class StringTest4 { public static void main(String[] args) { String fullName = GetData.getString("Enter the name"); if (fullName.equalsIgnoreCase(“Joe")) { System.out.println("Hey - it's Joe Spludnik."); } else { System.out.println("Hello " + fullName); } Enter the name: JOE Hey - it's Joe Spludnik. Enter the name: joE Hey - it's Joe Spludnik.

How do we convert a number to a String? public class StringTest4 { public static void main(String[] args) { int oldBadge = GetData.getInteger("Enter the badge number"); String newBadge = "A" + String.valueOf(oldBadge * 10); System.out.println("The new badge number is " + newBadge); } Our company is creating new badges for employees. The new badge will contain the existing badge number (which is stored as an integer like 1234) that will now be proceeded with an "A" and appended with a zero. For example, the badge 1234 becomes A12340.

How do we convert a string to a number? public class StringTest5 { public static void main(String[] args) { String oldBadge = GetData.getString("Enter the badge number"); oldBadge = oldBadge.substring(1); int tempBadge = Integer.parseInt(oldBadge) / 5; String newBadge = "A" + String.valueOf(tempBadge); System.out.println("The new badge number is " + newBadge); } We made a mistake. All the number portions of the new badges must be divided by 5. Therefore, the badge A5000 should now become A1000.

import java.util.*; public class StringTest { public static void main(String[] args) { GetInput in = new GetInput(); String firstName=null; String middleName=null; String lastName=null; String name = in.getString("Enter name"); StringTokenizer fullname = new StringTokenizer(name); switch (fullname.countTokens()) { case 1: lastName = fullname.nextToken(); break; case 2: firstName = fullname.nextToken(); lastName = fullname.nextToken(); break; case 3: firstName = fullname.nextToken(); middleName = fullname.nextToken(); lastName = fullname.nextToken(); break; default: System.out.println("Invalid Name"); } Tokenizer

(continued) lastName = initialCaps(lastName); firstName = initialCaps(firstName); middleName = initialCaps(middleName); if (lastName != null) System.out.println("LAST: "+lastName); if (firstName != null) System.out.println("FIRST: "+firstName); if (middleName != null) System.out.println("MIDDLE: "+middleName); } public static String initialCaps(String data) { if (data != null) { data = data.toUpperCase(); data = data.charAt(0) + data.substring(1).toLowerCase(); } return data; }

Homework 3 Part 6 We will be writing a program that translates a phone number into the English equivalent English words for the digits. So, for example, the phone number would become: Enter phone number: Area code: eight one six Prefix: nine four one Suffix: zero four three zero Enter phone number: DEVRY Area code: toll free eight zero zero Prefix: seven three Suffix: three three eight seven nine Toll free numbers begin with 800 or 888.

First of all, here's the test program public class TestPhone { public static void main(String[] args) { PhoneNumber number = new PhoneNumber(); number.setNumber(GetData.getString("Enter phone number")); System.out.println("Area Code: " + number.getAreaCode()); System.out.println("Prefix: " + number.getPrefix()); System.out.println("Suffix: " + number.getSuffix()); }

PhoneNumber class You will need to add four private attributes: number, areaCode, prefix, and suffix. All will be Strings. Add a setNumber method. Add a breakUpNumber method that will split the number up into its three components and place them in the respective attributes. Add three get methods for areaCode, prefix, and suffix. Each of these methods will call the convertString method and pass its respective attribute to convertString. Return the string that convertString returns to the calling program. getAreaCode should also add "toll free" to the beginning of the returned string if the areaCode is "888" or "800".

PhoneNumber class continued convertString will be passed a String containing either the areaCode, prefix or suffix. Determine the length of this String and loop through it character by character. Send each character to the convertNumber method (supplied) which will return a word containing the converted character. Build a return String made up of these words. convertNumber will be passed a character and convert it into a word. convertString will use this to convert a character such as "1" to the word "one", or the character "R" to the word "seven".

convertNumber method in PhoneNumber class public String convertNumber(char letter) { String word = ""; //word to be returned switch (letter) { case '1': word = "one"; break; case '2': case 'A': case 'B': case 'C': word = "two"; break; case '3': case 'D': case 'E': case 'F': word = "three"; break; case '4': case 'G': case 'H': case 'I': word = "four"; break; case '5': case 'J': case 'K': case 'L': word = "five"; break; case '6': case 'M': case 'N': case 'O': word = "six"; break; case '7': case 'P': case 'Q': case 'R': case 'S': word = "seven"; break; case '8': case 'T': case 'U': case 'V': word = "eight"; break; case '9': case 'W': case 'X': case 'Y': case 'Z': word = "nine"; break; case '0': word = "zero"; break; } return word; }

UML for Homework 3 Part 6