0% found this document useful (0 votes)
13 views10 pages

Week 5 - Lecture 1 - Chapter 9

Uploaded by

husseinalayan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Week 5 - Lecture 1 - Chapter 9

Uploaded by

husseinalayan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

CHAPTER 9 STRINGS

9.2 The String Class


9.2.8 Finding a Character or a Substring in a String
9.2.10 Converting Characters and Numeric Values to
Strings
Converting Strings to Integer and Double using
Integer.parseInt( ) and Double.parseDouble( )
Solve programming exercises 9.2 and 9.4
9.2.8 FINDING A CHARACTER OR A
SUBSTRING
2 IN A STRING

java.lang.String
+indexOf(ch: char): int Returns the index of the first occurrence of ch in the string.
Returns -1 if not matched.
+indexOf(ch: char, fromIndex: Returns the index of the first occurrence of ch after fromIndex in
int): int the string. Returns -1 if not matched.
+indexOf(s: String): int Returns the index of the first occurrence of string s in this string.
Returns -1 if not matched.
+indexOf(s: String, fromIndex: Returns the index of the first occurrence of string s in this string
int): int after fromIndex. Returns -1 if not matched.
+lastIndexOf(ch: int): int Returns the index of the last occurrence of ch in the string.
Returns -1 if not matched.
+lastIndexOf(ch: int, Returns the index of the last occurrence of ch before fromIndex
fromIndex: int): int in this string. Returns -1 if not matched.
+lastIndexOf(s: String): int Returns the index of the last occurrence of string s. Returns -1 if
not matched.
+lastIndexOf(s: String, Returns the index of the last occurrence of string s before
fromIndex: int): int fromIndex. Returns -1 if not matched.
FINDING A CHARACTER OR A
SUBSTRING
3
IN A STRING

"Welcome to Java".indexOf('W') returns 0.


"Welcome to Java".indexOf('x') returns -1.
"Welcome to Java".indexOf('o', 5) returns 9.
"Welcome to Java".indexOf("come") returns 3.
"Welcome to Java".indexOf("Java", 5) returns 11.
"Welcome to Java".indexOf("java", 5) returns -1.
"Welcome to Java".lastIndexOf('a') returns 14.
9.2.10 CONVERTING CHARACTERS AND
NUMERIC
4
VALUES TO STRINGS
9.2.10 CONVERTING CHARACTERS AND
NUMERIC
5
VALUES TO STRINGS

The String class provides several static valueOf methods for converting a
character, an array of characters, and numeric values to strings. These methods
have the same name valueOf with different argument types char, char[], double,
long, int, and float.
For example, to convert a double value to a string, use String.valueOf(5.44). The
return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’.
CONVERTING STRINGS TO INTEGER AND
DOUBLE
6
USING INTEGER.PARSEINT( ) AND
DOUBLE.PARSEDOUBLE( )

To convert a String into an integer we use the method Integer.parseInt()


and into a double we use the method Double.parseDouble().

String s1 = "12", s2 = "12.5";


int n = Integer.parseInt(s1);
double m = Double.parseDouble(s2);

As a result n will contain the integer value 12 and m will contain the
double value 12.5.
Note that if s1 or s2 contain non digit characters then an error will
occur. Of course s2 can contain the dot.
PROGRAMMING EXERCISE 9.4 IN YOUR
TEXTBOOK
7
PAGE 363: OCCURRENCES OF A
SPECIFIED CHARACTER IN A STRING

Write a method that finds the number of occurrences of a specified character in a


string using the following header:
public static int count(String str, char a)
For example, count("Welcome", 'e') returns 2.

Write a test program (main method) that prompts the user to enter a string
followed by a character and displays the number of occurrences of the character in
the string.
SOLUTION OF PROGRAMMING EXERCISE 9.4
import java.util.Scanner;
8
public class Exercise09_04 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");

String s = input.next();

System.out.print("Enter a character: ");

char ch = input.next().charAt(0);

int counter = count(s, ch);

System.out.println(counter);

public static int count(String str, char ch) {

int counter = 0;

for (int i = 0; i < str.length(); i++)

if (str.charAt(i) == ch)

counter++;

return counter;

}
PROGRAMMING EXERCISE 9.6 IN YOUR
TEXTBOOK
9
PAGE 363: COUNTING THE
LETTERS IN A STRING

Write a method that counts the number of letters in a


string using the following header:
public static int countLetters(String s)
Write a test program that prompts the user to enter a string and
displays the number of letters in the string.
SOLUTION OF PROGRAMMING EXERCISE 9.6
import java.util.Scanner;
10
public class Exercise09_06 {

public static void main(String[] args) {

// Prompt the user to enter a string

Scanner input = new Scanner(System.in);

System.out.print("Enter a string: ");

String s = input.nextLine();

System.out.println("Number of letters is " + countLetters(s));

public static int countLetters(String s) {

int count = 0;

for (int i = 0; i < s.length(); i++)

if ((s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') ||

(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'))

count++;

return count;

You might also like