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,360 points
49 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 by Karan
• 6,670 points

Related Questions In Java

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,050 points
260 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,880 points
131 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 40,990 points
209 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,540 points
57 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
100 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,190 points
702 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,190 points
293 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,190 points
54 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,050 points
887 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 28, 2018 in Java by curious
• 560 points
74 views