The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Strings Testing for equality with strings.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 7:
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Fundamental Programming Structures in Java: Strings.
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.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
Data Input and Output Using the Library Software CSC 1401: Introduction to Programming with Java Lecture 4 – Part 1 Wanda M. Kunkle.
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CHAPTER 8 CHARACTER AND STRINGS
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7: Characters, Strings, and the StringBuilder.
Getting Started Java Fundamentals CSC207 – Software Design Summer 2011 – University of Toronto – Department of Computer Science.
Chapter 2 Variables.
More Strings CS303E: Elements of Computers and Programming.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CSC Programming for Science Lecture 8: Character Functions.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington.
Constants, Data Types and Variables
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Java String Methods - Codehs
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Strings, StringBuilder, and Character
IDENTIFIERS CSC 111.
Chapter 7: Strings and Characters
Unit-2 Objects and Classes
Introduction to C++ Programming
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Chapter 2 Variables.
STRINGS BY: DANIEL, JUSTIN, PANI.
Pre-AP® Computer Science Quiz
Presentation transcript:

The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle

2 Data Types in Java Recall from week 2 that there are two main kinds of types in Java: Recall from week 2 that there are two main kinds of types in Java: –Primitive type  Stores a single piece of data of a specific type  Some primitive types and the kinds of data they store are: –int – a 32-bit integer (A bit is a 0 or 1.) –float – a 32-bit floating point (decimal) number –double – a 64-bit floating point (decimal) number –char – a single character (e.g., ‘Y’)

3 Data Types in Java Class type Class type –Stores multiple pieces of data and provides methods for manipulating that data –Some class types are:  String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan”  input – stores and manipulates text entered at the keyboard –Variables that are class types are called objects.

4 Declaring String Objects We declare String variables (technically, objects ) basically the same way that we declare variables of other types. We declare String variables (technically, objects ) basically the same way that we declare variables of other types. –Examples:  String first_name, last_name, full_name; We assign values to String objects basically the same way that we assign values to variables of other types. We assign values to String objects basically the same way that we assign values to variables of other types. –Examples:  first_name = “Darth”;  last_name = “Vader”;  full_name = first_name + “ ” + last_name ;

5 Inputting Strings Strings Strings –Use the readname() method to input a string consisting of letters, digits, and underscores.  The method reads until it encounters a character that is not a letter, digit, or underscore.  For example, given the input “Ah! I see.” the statement: String name = in.readname(); would store “Ah” in name. –Use the readword() method to input a string consisting of nonblank characters.  The method reads until it encounters a blank character (Blank characters include the space and newline characters.).  For example, given the input “Ah! I see.” the statement: String word = in.readword(); would store “Ah!” in word.

6 Inputting Strings Strings Strings –Use the readline() method to input a string consisting of nonblank as well as blank characters.  The method reads until it encounters the newline character (which it reads but does not store).  For example, given the input “Ah! I see.” the statement: String line = in.readline(); would store “Ah! I see.” in line. –Use the readln() method to input a string and skip it.  The method reads until it encounters the newline character; it skips all characters, including the newline.  For example, the statement: in.readln(); would read and skip any input string ending in newline.

7 String Methods String methods can be used to manipulate the values stored in String objects. String methods can be used to manipulate the values stored in String objects. Java’s creators at Sun Microsystems provided many such methods, viewable at: Java’s creators at Sun Microsystems provided many such methods, viewable at: – We will look at a small subset of these methods. We will look at a small subset of these methods.

8 String Methods length() length() –Returns the length of the String object –Example:  output out = new output(); String first_name = “Darth”; int name_length = first_name.length(); out.writeln(name_length); // Displays 5

9 String Methods toUpperCase() toUpperCase() –Returns a string with same characters as the calling String object, but converted to uppercase –Example:  output out = new output(); String first_name = “Darth”; out.writeln(first_name.toUpperCase()); // Displays // DARTH

10 String Methods toLowerCase() toLowerCase() –Returns a string with same characters as the calling String object, but converted to lowercase –Example:  output out = new output(); String first_name = “Darth”; out.writeln(first_name.toLowerCase());// Displays // darth –Question:  Do you think the original string has been altered? –How could we go about checking this?

11 Sample Program Now let’s look at a sample program that demonstrates using these 3 methods: Now let’s look at a sample program that demonstrates using these 3 methods: –StringTypeDemo1.java StringTypeDemo1.java

12 String Methods equals(otherString) equals(otherString) –Returns true if the calling String object and otherString are equal; otherwise, returns false –Example:  output out = new output(); String response = “yes”; if (response.equals(“Yes”)) out.writeln(“The responses are equivalent.”); else out.writeln(“The responses are NOT equivalent.”); // What do you think is displayed?

13 String Methods equalsIgnoreCase(otherString) equalsIgnoreCase(otherString) –Returns true if the calling String object and otherString are equal when case is ignored; otherwise, returns false –Example:  output out = new output(); String response = “yes”; if (response.equals(“YES”)) out.writeln(“The responses are equivalent.”); else out.writeln(“The responses are NOT equivalent.”); // What do you think is displayed?

14 Sample Program Now let’s look at a sample program that demonstrates using one of these methods (Aside: You’ve seen it before.): Now let’s look at a sample program that demonstrates using one of these methods (Aside: You’ve seen it before.): –examAverager.java examAverager.java

15 String Methods substring(start) substring(start) –Returns the substring of the calling String object from position start to the end of the calling object –Example:  String street_name, street_address; street_address = “3141 Chestnut Street”; street_name = street_address.substring(5);

16 String Methods substring(start, end) substring(start, end) –Returns the substring of the calling String object from position start through, but not including, position end of the calling object –Example:  String street_number, street_address; street_address = “3141 Chestnut Street”; street_number = street_address.substring(0, 4);

17 String Methods indexOf(aString) indexOf(aString) –Returns the position of the first occurrence of aString in the calling String object; if aString is not found, returns -1 –Example:  String street_number, street_name, street_address; int spacePosition; street_address = “3141 Chestnut Street”; // Use the location of the space to break apart the street address spacePosition = street_address.indexOf(" "); street_name = street_address.substring(spacePosition + 1); street_number = street_address.substring(0, spacePosition);

18 String Methods indexOf(aString, start) indexOf(aString, start) –Returns the position of the first occurrence of aString in the calling String object that occurs at or after position start; if aString is not found, returns -1 –Example:  String street_address; int periodPosition; street_address = “3141 Chestnut Street”; // Locate a period, if present periodPosition = street_address.indexOf(“.”, 0); // What value does the above code return?

19 Sample Program Now let’s look at a sample program that demonstrates using at least 3 of these methods: Now let’s look at a sample program that demonstrates using at least 3 of these methods: –StringTypeDemo2.java StringTypeDemo2.java

20 String Methods charAt(position) charAt(position) –Returns the character in the calling String object at position –Example:  String street_address; char spaceChar; street_address = “3141 Chestnut Street”; spaceChar = street_address.charAt(4);