-4

What can I do to modify this, is there any java function for that? What needs to be done so that it only accepts characters and returns an error message for other data types?

import java.util.*;
    public class problem5
    {
      public static void main(String[] args)
      {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a letter from the alphabet: ");
        char letter = in.next(".").charAt(0);
        char vowels[] = {'A','E','I','O','U','a','e','i','o','u'};
        int vowelcount = 0;
        for (int i = 0; i < 10; i++)
        {
          if (vowels[i] == letter)
          {
            vowelcount++;
          }
        }
        if (vowelcount > 0)
        {
          System.out.println("You entered a vowel.");
        }
        else
        {
          System.out.println("You entered a consonant.");
        }
      }
    }
  • Well, everything is character in a `String`. You want to check if you only get alphabetic character maybe ? – AxelH Sep 29 '17 at 08:36
  • https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner – Tom Sep 29 '17 at 08:36
  • If I need to reject two character strings and above; and numbers what should I add? – Nico Dela Cruz Sep 29 '17 at 08:40
  • Possible duplicate of [how to make jtextfield only accept characters in netbeans](https://stackoverflow.com/questions/34377607/how-to-make-jtextfield-only-accept-characters-in-netbeans) – John Joe Sep 29 '17 at 08:41
  • "_I need to reject two character strings and above_" You mean you have a list of `rejectedChar` ? Or you are rejecting `String` that have more than 1 character ? (String.length > 1) – AxelH Sep 29 '17 at 08:41
  • no I need to reject input that has more than 1 char – Nico Dela Cruz Sep 29 '17 at 08:42
  • Check the length of the String ... ? – AxelH Sep 29 '17 at 08:42

2 Answers2

1

I need to reject input that has more than 1 char – Nico Dela Cruz

You just need to check the length of your input

String input = in.next(".");
if(input.length == 1){
    char letter = input.charAt(0);
    ...
}

Add an else if you want to add an error message of some sort.

To check the input to only accept letter, you have Character.isLetter(char) to check every "letter" in UNICODE for you.

If you want to only accept a range of a-z and/or A-Z, you can do it yourself with an if condition or using regex.

AxelH
  • 13,322
  • 2
  • 21
  • 50
  • thanks! but how do I reject numbers/integers as inputs? – Nico Dela Cruz Sep 29 '17 at 08:45
  • @NicoDelaCruz You just said you want to reject `String` too long .... for the rest of the reject, check the duplicates proposed or the answer above. This is a recurrent question – AxelH Sep 29 '17 at 08:46
0

Wrap your loop in a statement such as:

if (Character.isLetter(letter)){

and put and else clause at the end for your error

Edit:

OP has changed their question slightly, so you can either:

-Accept only the first character entered:

char letter = in.next().trim().charAt(0);

-Or as AxelH said above, only proceed if user enters one char:

if(input.length == 1){
    char letter = input.charAt(0);
achAmháin
  • 3,944
  • 3
  • 12
  • 39