220 likes | 311 Views
The Java String Type. CSC 1401 : Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle. Data 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
E N D
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle
Data 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’)
Data Types in Java • 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.
Declaring String Objects • 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. • Examples: • first_name = “Darth”; • last_name = “Vader”; • full_name = first_name + “ ” + last_name ;
Inputting 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.
Inputting 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.
String Methods • 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: • http://java.sun.com/javase/6/docs/api/index.html • We will look at a small subset of these methods.
String Methods • 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
String Methods • 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
String Methods • 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?
Sample Program • Now let’s look at a sample program that demonstrates using these 3 methods: • StringTypeDemo1.java
String Methods • 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?
String Methods • 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?
Sample Program • Now let’s look at a sample program that demonstrates using one of these methods (Aside: You’ve seen it before.): • examAverager.java
String Methods • 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);
String Methods • 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);
String Methods • 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 addressspacePosition = street_address.indexOf(" ");street_name = street_address.substring(spacePosition + 1);street_number = street_address.substring(0, spacePosition);
String Methods • 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 presentperiodPosition = street_address.indexOf(“.”, 0);// What value does the above code return?
Sample Program • Now let’s look at a sample program that demonstrates using at least 3 of these methods: • StringTypeDemo2.java
String Methods • 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);