Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Methods: length substring

Similar presentations


Presentation on theme: "String Methods: length substring"— Presentation transcript:

1 String Methods: length substring

2 To use scientific notation, use the letter e followed by a power of 10
To use scientific notation, use the letter e followed by a power of 10. For instance, 3.65e+9 means 3.65 * 109 or 3,650,000,000. 3.65e-9 means 3.65 * 10-9 or A byte is typically 8 bits. A bit is a binary digit, taking a logical value of either "1" or "0" (also referred to as "true" or "false" respectively). Binary digits are a basic unit of information storage.

3 Strings are not primitive data types
Strings are not primitive data types. They are actually a class with methods and constructors.

4

5

6 Examples of length method
public int getNameLength(String name) { int nameLength; nameLength = name.length(); return nameLength; } Another way to write the same method: return name.length(); The first example uses a local variable.

7 Example of length method
public void printStudentId(String id) { if (id.length() > 0) System.out.println("Student id: " + id); } else System.out.println("The student has not been assigned an id.");

8 Example of substring method
public String getAreaCode(String phoneNumber) { String areaCode; areaCode = phoneNumber.substring(0, 3); return areaCode; } Another way to write the same method: return phoneNumber.substring(0, 3);

9 Exercise Add conditional statements to the constructor of Student to print an error message if either the length of the fullName parameter is less than four characters or the length of the studentID parameter is less than three characters. The constructor should still use those parameters to set the name and id fields even if the error message is printed.

10 Exercise Modify the getLoginName method of student so that it always generates a login name, even if either of the name and id fields is not strictly long enough. For strings shorter than the required length, use the whole string.


Download ppt "String Methods: length substring"

Similar presentations


Ads by Google