Check String Start with Varargs in Java


Create a Java program that checks if a set of strings starts with a specific character using varargs

The class contains a method called checkStartsWithCharacter, which takes a character character and a variable number of strings as input. Inside this method, it iterates through each string in the input array and checks if it starts with the specified character. It does this by checking if the length of the string is greater than 0 (to avoid errors) and if the first character of the string matches the specified character. It then prints out whether each string starts with the specified character or not.

The main method is the entry point of the program. Inside main, it calls the checkStartsWithCharacter method with the character 'J' and several strings ("Java", "JavaScript", "Python") as arguments.

Finally, the program prints out the result for each string, indicating whether it starts with the character 'J' or not.

Source Code

public class StartsWithChecker
{
	static void checkStartsWithCharacter(char character, String... strings)
	{
		for (String str : strings)
		{
			boolean startsWith = str.length() > 0 && str.charAt(0) == character;
			System.out.println(str + " starts with character 'J'? " + startsWith);
		}
	}
 
	public static void main(String[] args)
	{
		checkStartsWithCharacter('J', "Java", "JavaScript", "Python");
	}
}

Output

Java starts with character 'J'? true
JavaScript starts with character 'J'? true
Python starts with character 'J'? false

Example Programs