Find Second Occurrence of Character in String Java

Find Second Occurrence of Character in String Java | In a given string, there might be many occurrences of a character. In this post, we will find second occurrence of character In string Java. To solve this problem, first, we have to understand two forms of the indexOf() method.

To do this we can use many methods available in the Java library. Like other languages, Java also provides many string manipulating methods which have made programmers easy.  Here we use one such method called indexOf().

Method Syntax:- public int indexOf(int ch)

  • Parameters: The character for which we want to find the index.
  • Returns: Index of the first occurrence of the character from start. It returns -1 if the given character doesn’t exist.

The above method takes a character that specifies the character needed to be searched and returns the index where it exists. Let us see an example:-

String str = "Know Program";
System.out.println(str.indexOf('o'));

Output:-

2

This just takes the character parameter and returns the first occurrence of the character ‘o’.

Method Syntax:- public int indexOf(int ch, int fromIndex)

  • Parameters: ch – Character for which we want to find the index. fromIndex – the index to start the search from.
  • Returns: Index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.

Example of indexOf(int ch, int fromIndex) method:-

String str = "Know Program";
System.out.println(str.indexOf('o', 4));

Output:-

7

The below code snippets return the first occurrence of the character ‘a’ from index 4 in the given string “Know Program”.

Find the Second Occurrence Of Character in String Java using indexOf()

Now to find the second occurrence of a character in string Java we can use both of these indexOf() methods so that it fetches the second occurrence. Observe the below code. 

String str = "Know Program";
char ch = 'o';
System.out.println(str.indexOf(ch, str.indexOf(ch) + 1));

Output:

7

Observe the first occurrence of ‘0’ is in the 2nd position. Hence the str.indexOf(ch) returns value 2 which is passed to the previous method as a parameter. The str.indexOf(ch, 3) is called which returns the first occurrence of ‘o’ from the 3rd position hence the code returns 7.

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);

      System.out.print("Enter String: ");
      String str = scan.nextLine();
      System.out.print("Enter a character: ");
      char ch = scan.next().charAt(0);

      int index = str.indexOf(ch, str.indexOf(ch) + 1);

      System.out.println("Index of the second occurrence of " 
                         + " character \'" + ch + "\' is: "+ index);
      scan.close();
   }
}

Output:-

Enter String: Know Program
Enter a character: r
Index of the second occurrence of character ‘r’ is: 9

Test-case when character exists only once:-

Enter String: Know Program
Enter a character: n
Index of the second occurrence of character ‘n’ is: -1

Test-case when a character doesn’t exist in the given string:-

Enter String: Know Program
Enter a character: H
Index of the second occurrence of character ‘H’ is: -1

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *