Java Program to Find First Character Occurrence in a String

Write a Java Program to Find First Character Occurrence in a String with an example. In this java First Character Occurrence example, we used the While loop to iterate the firstCharStr from start to end. We used the String charAt function on the firstCharStr to get the character at each index position within the while loop. 

Next, we compare that character with the user given ch character to check whether they are equal. If True, we increment the flag value to 1, and the break statement will exit from the loop. Next, we used the Java If Else statement to check the flag value equals 0. If it is False, then print the character index position.

import java.util.Scanner;

public class FirstCharOccurence1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		int i = 0, flag = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Find First Char Occur =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		while(i < firstCharStr.length())
		{
			if(firstCharStr.charAt(i) ==  ch) {
				flag++;
				break;
			}
			i++;
		}
		if(flag == 0) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe First Character Occurrence of %c at %d position", 
					ch, i);
		}
	}

}

Java First Character Occurrence in a String using a while loop output

Enter String to Find First Char Occur =  hello

Enter the Character to Find =  l

The First Character Occurrence of l at 2 position

Java Program to Find First Character Occurrence in a String using For Loop

import java.util.Scanner;

public class FirstCharOccurence2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		int i, flag = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Find First Char Occur =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		for(i = 0; i < firstCharStr.length(); i++)
		{
			if(firstCharStr.charAt(i) ==  ch) {
				flag++;
				break;
			}
		}
		if(flag == 0) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe First Character Occurrence of %c at %d position", 
					ch, i);
		}
	}

}
Enter String to Find First Char Occur =  java

Enter the Character to Find =  a

The First Character Occurrence of a at 1 position

In Java Programming, there is a string function called indexOf. This indexOf function returns the index position of the first occurrence of a character in a string. Next, we used the string charAt function not return the character at that index position.

import java.util.Scanner;

public class FirstCharOccurence3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find First Occurrence =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		int x = firstCharStr.indexOf(ch);
		
		System.out.format("\nThe First Character Occurrence of %c at %d position", 
				firstCharStr.charAt(x), x);
	}
}
Java Program to Find First Character Occurrence in a String 3

The Java StringBuilder and StringBuffer also have the indexOf and charAt function to return the string’s first character occurrence. In this Java example, we converted the given string to StringBuilder, StringBuffer, and returns the output.

import java.util.Scanner;

public class FirstCharOccurence4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find First Occurrence =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(firstCharStr);
		int x = sb.indexOf(String.valueOf(ch));
		
		System.out.format("\nThe First Character Occurrence of %c at %d position", 
				sb.charAt(x), x);
		
		StringBuffer sbuff = new StringBuffer(firstCharStr);
		int y = sb.indexOf(String.valueOf(ch));
		
		System.out.format("\nThe First Character Occurrence of %c at %d position", 
				sbuff.charAt(y), y);
	}
}
Please Enter String to find First Occurrence =  tutorial gateway

Enter the Character to Find =  a

The First Character Occurrence of a at 6 position
The First Character Occurrence of a at 6 position

Please refer to the following string programs.

  1. Count the Total Occurrence of a Character in a String
  2. Find All Occurrences of a Character in a String
  3. Replace First Character Occurrence in a String
  4. Remove First Character Occurrence in a String
  5. Last Character Occurrence in a String
  6. Replace Last Character Occurrence in a String
  7. Remove the Last Character Occurrence in a String
  8. First and Last Character in a String
  9. Remove The First and Last Character in a String
  10. Maximum Occurring Character in a String
  11. Minimum Occurring Character in a String
  12. Frequency of each Character in a String
  13. Remove All Occurrences of a Character in a String