How to check if a string has only letters

0 votes

The point is to have a String read and to verify if it contains only letters. For example "qwerty123" would not be acceptable.

Aug 29, 2018 in Java by bug_seeker
• 15,520 points
3,192 views

2 answers to this question.

0 votes
The following is the code for your answer:

public static void main(String[] args)

{

Scanner reader = new Scanner(System.in);  

System.out.println("Enter a string: ");

String name = reader.nextLine();

reader.close();

char[] chars = name.toCharArray();

int i=0;

    for (char c : chars)

        if(!Character.isLetter(c))

          i++;

    if (i>0)

    System.out.println("The string does not contain only letters");

    else

    System.out.println("The string contains only letters");

}
answered Aug 29, 2018 by curious
• 560 points
0 votes

You could probably use Java 8 lambda expressions, they are fast and simple.

boolean allLetters = someString.chars().allMatch(Character::isLetter);
answered Aug 30, 2019 by Karan
• 19,610 points

Related Questions In Java

0 votes
3 answers

How to check if a String is numeric in Java?

Check if a string is numeric public class ...READ MORE

answered Dec 29, 2020 in Java by Carlos
2,724 views
0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,140 points
3,900 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
1,349 views
0 votes
1 answer

How do I check if a string contains a substring in Java?

Hi@akhtar, The first and foremost way to check ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,460 points

edited Jul 5, 2023 by Khan Sarfaraz 56,072 views
0 votes
0 answers

How to check whether a string contains a substring in JavaScript?

There doesn't seem to be a String.contains() ...READ MORE

Sep 21, 2022 in Java by Nicholas
• 7,760 points
453 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,442 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,220 points
4,661 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
953 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,616 views
0 votes
2 answers

How do we convert only the second letter of every word in a string to uppercase ?

The following code will perform your desired ...READ MORE

answered Aug 29, 2018 in Java by curious
• 560 points
11,858 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP