Switch, Strings, and ArrayLists in Java

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
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.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.
Introduction to Java Java Translation Program Structure
Chapter 7: Characters, Strings, and the StringBuilder.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Strings, Characters and Regular Expressions
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
String class.
EKT 472: Object Oriented Programming
String String Builder.
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
Modern Programming Tools And Techniques-I Lecture 11: String Handling
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
Part a: Fundamentals & Class String
An overview of Java, Data types and variables
Tonga Institute of Higher Education
Can store many of the same kind of data together
Microsoft Visual Basic 2005: Reloaded Second Edition
CS2011 Introduction to Programming I Strings
String methods 26-Apr-19.
Strings in Java.
Visual Programming COMP-315
More JavaScript B. Ramamurthy 5/7/2019.
Comparing Python and Java
Objects with ArrayLists as Attributes
String Manipulation.
Strings in Java Strings in Java are also a reference type.
Pre-AP® Computer Science Quiz
Unit-2 Objects and Classes
What We Want To Do User enters: Mary Smith
Presentation transcript:

Switch, Strings, and ArrayLists in Java CS102 Written by Robert Carver Edited by Dean Zeller

Switch vs Else-If Statements These are equivalent statements:

Switch General Form: switch(variable) case (value): (statements) break A switch statement includes cases based on the value of a specific variable. Each case is assigned to a different value and has statements it will execute as well as a break command. In most switch statements there is a default case that executes if none of the other cases execute. switch(variable) case (value): (statements) break default:

Strings – defining, +, length Defining Strings: A String variable is defined as String (name). The string does not need content upon being defined. String concatenation: String objects can be concatenated to form other String objects. String string3 = string1 + string 2; String length: string3.length() returns the amount of characters in string3 as an int value.

Strings – Escape Characters Common Escape Characters: \b Inserts a backspace \t Inserts a horizontal tab \n Inserts a newline \f Inserts a form \r Inserts a carriage return \” Inserts a double quote (“) \’ Inserts a single quote (‘) \\ Inserts a backslash (\) Escape characters are symbols that we cannot normally type into a string because of formatting reasons.

Strings – charAt, compareTo charAt method: Returns a character at the supplied string index. Format: string.charAt(i) compareTo method: Compares strings based on the Unicode value of each character of the string, and returns a value. if s1 > s2, it returns a positive number   if s1 < s2, it returns a negative number   if s1 == s2, it returns 0   Format: s1.compareTo(s2)

Strings – concat and equals concat method: The concat method concatenates two strings together. Format: string1 = string1.concat(string2) equals method: The equals method compares two strings and returns a Boolean value. Is case sensitive, equalsIgnoreCase is not. Format: Boolean value = string1.equals(string2)

Strings – indexOf, toUpper, toLower indexOf method: Returns the index of the first occurrence of a character in a string as an int. Format: string.indexOf(char) lastIndexOf method: Returns the index of the last occurrence of a character in a string as an int. Format: string.lastIndexOf(char) toUpper and toLower methods: Changes the characters of a string to upper or lower case Format: string.toUpperCase(), string.toLowerCase()

Strings – replace and subString replace method: Replaces the selected chars, or char sequence, in a string with another char or char sequence. Format: string.replace(char1,char2) subString method: Selects chars past a give index and returns the new substring. Format: string.substring(index) Creates a new substring selection in the given range. Format: string.substring(index1, index2)

Strings – trim trim method The trim method removes any trailing or leading whitespaces from a string. Format: string.trim()

ArrayList – defining, get, and set See example file for implementations of these methods with arrays. Declaration: List<type> name = new ArrayList<type>() Item retrieval: name.get(index) , returns item from supplied index. Item reassignment: name.set(index, content), assigns item at index to new content.

ArrayList – remove, size, contains See example file for implementations of these methods with arrays. Item removal: name.remove(index), removes item at supplied index. List Size: name.size(), returns int value of size of list. Contains method: name.contains(search), searches the list for the supplied item and returns a Boolean value.

ArrayList – indexOf, isEmpty See example file for implementations of these methods with arrays. indexOf: name.indexOf(search), searches the list for the supplied item and returns the index of the item as an int. isEmpty: name.isEmpty(), determines if list contains any elements. Returns a Boolean value.

Credits Content and visual organization by Robert Carver Concept and final editing by Dean Zeller Reference: Java An Introduction to Problem Solving, (8th edition) by Walter Savitch