To check if the first character of a string in Java is a number, you can use the isDigit()
method of the Character
class.
Here's an example of how to do it:
public class Main { public static void main(String[] args) { String input = "123abc"; // Check if the first character is a number boolean isNumber = Character.isDigit(input.charAt(0)); System.out.println(isNumber); // prints "true" } }
This code creates a string and then uses the charAt()
method to get the first character of the string. The isDigit()
method is then used to check if the character is a digit.
You can use a similar approach to check if the first character of a string is any other type of character, such as a letter or a symbol. The Character
class provides several methods for checking the type of a character, including isLetter()
, isLetterOrDigit()
, and isUpperCase()
.