chevron_left
358 points
19 13 11

There are so many solutions to this problem. You can check this by using the regular expressions or the ASCII codes but the simplest yet easiest way to do this is by using isLetter() and isDigit() methods in Java.

isLetter():

This method is defined in Character class and it returns true if the character at the specified location in a string is a letter. The syntax for using this method is as follows:

if(Character.isLetter(character))
{ . . .}

isDigit():

This method is also defined in Character class and it also returns true if the character at the specified location in a string is a number or digit. The syntax for using this method is as follows:

if( Character.isDigit( character ) )
{ . . . }

The following program demonstrates how to check if a string contains letters and numbers. Please refer to the comments to understand the code:

import java.util.*;
public class Main
{
   
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a word");  //prompting user to enter a string
        String str = sc.nextLine();  //taking input from user
        int letter_flag = 0;   //flag for letter
        int digit_flag = 0;   //flag for number
        
        for(int i = 0; i < str.length(); i++)   //looping through the string
        {
            if(Character.isDigit(str.charAt(i)))  //checking if the character at certain position is letter
            {
                digit_flag = 1;   //if yes, the flag is turned to 1
            }
            else if(Character.isLetter(str.charAt(i)))   //checking if the character at the certain position is number
            {
                letter_flag = 1;   //if yes, the flag is turned to 1
            }
            else
            {
                break;
            }
        }
        
        if(letter_flag == 1 && digit_flag == 1)   //checking if the string contains numbers and letters
        {
            System.out.println("String contains numbers and letters"); 
        } 
        else if(letter_flag == 1)   //checking if the string contains only letters
        {
            System.out.println("String does not contain any number");
        }
        else if(digit_flag == 1)   //checking if the string contains only digits
        {
            System.out.println("String does not contain any letter");
        }
        else   //if the string does not contain numbers or letters, this block executes
        {
            System.out.println("String does not contain any letter or number");
        }
        
    }
}


Output:

Please enter a word
Th15
String contains numbers and letters

 

More Posts

Check if a string contains special characters java MallikaShaik672 - Nov 4, 2020
Calculate sum of all numbers present in a string java Hafsa_Rao - Jul 17
Javascript check if string contains only numbers amna - Oct 19, 2020
How to check if a character is a number in java? Gavin - Mar 18, 2020
How to check if a string is empty java offonime - Jul 8
How to check if a string has a certain character java? Hafsa_Rao - Jul 4
Write a java program to count the letters, spaces, numbers and other characters of an input string. Jeevani - Jun 7
Java check if string contains character sakshi - May 28
How to generate random numbers in java within range Hasnain_khan - Oct 13, 2020
Javascript check if string contains only numbers Lalit Kumar - Sep 28, 2020