Checking Character only in String - Java 2024 - MNC - Technical Interview

 Checking if the String contains Character only

    Get the input from the user using Scanner class. Then create a method to check the following condition.

1. Check if the string is null or empty, if it is directly return false.

2. Iterate the String using for loop inside the for loop, use the if for condition checking.

3. Condition is check the Character only, for this use the Character Wrapper class, in that we have isAlphabetic() method to check the character.

4. This method will return true or false based on it is character or not.


Code:

package com.dinatechy.challange;


import java.util.Scanner;


public class CheckCharacter {

public static void main(String[] args) {

//Check the String only contains Character or Not

Scanner scan = new Scanner(System.in);

System.out.print("Enter the String : ");

String data = scan.next();

scan.close();

boolean isChar = checkOnlyCharacter(data);

System.out.println(isChar ? "Yes" : "No");


}


private static boolean checkOnlyCharacter(String data) {

if(null == data || data.isEmpty())

return false;

int size = data.length();

for(int i=0; i<size; i++) { // DinaTechy

if(!Character.isAlphabetic(data.charAt(i))) {

return false;

}

}

return true;

}


}

Scenario 1 :

Input :

Enter the String : Dina$

Output:

No


Scenario 2:

Input :

Enter the String : DinaTechy

Output:

Yes

Follow us for more coding interview question and answer with completed Explaination. 

Comments

Popular posts from this blog

Finding Second largest number in Array - Java - TCS - Technical Interview

Object Class in Java | Article | Solution Maker - Blog