Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle."— Presentation transcript:

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

2 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 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 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 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 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 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: –http://java.sun.com/javase/6/docs/api/index.html http://java.sun.com/javase/6/docs/api/index.html We will look at a small subset of these methods. We will look at a small subset of these methods.

8 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 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 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 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 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 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 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 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 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 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 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 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 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);


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

Similar presentations


Ads by Google