-1
String[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

I have made the Array above including all the letters in the alphabet for a letter guessing game.

I am not sure how I could get an error message to appear if the user enters something outside of these values of the alphabet eg. a number? Any help would be greatly appreciated.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
java1234
  • 17
  • 1
  • 2
  • 1
    regex? like `if(value.equals("\\d"))` --> error. or even easier, vonvert the array to a list and perform a check like this: `if(!list.contains(value))` -->error – XtremeBaumer Apr 19 '17 at 13:25
  • 2
    `!input.matches("[a-z]")` should work... Forget the array of the alphabet – OneCricketeer Apr 19 '17 at 13:27

5 Answers5

4

You can convert it into a List and use contains, e.g.:

String[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
List<String> list = Arrays.asList(alpha);
System.out.println(list.contains("a"))

If you want case insensitive comparison then you can use toLowerCase().

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • This has worked in giving me the error, although it is outputting 26 errors when i try input a number, would you know how this could be fixed? Thanks for the help! – java1234 Apr 20 '17 at 12:54
  • Can you add an example? – Darshan Mehta Apr 20 '17 at 12:56
  • Is the line of code System.out.println(list.contains("a")) needed? I have tried an if statement instead which could be causing the repeated error messages. if(!list.contains(guess)){ System.out.println("Out of range"); – java1234 Apr 20 '17 at 13:02
  • `if(!list.contains(guess))` should work as long as `guess` is a String. – Darshan Mehta Apr 20 '17 at 13:24
1

You could use a character array than that one . Example is given here https://www.javatpoint.com/java-string-tochararray. Then you could use indexof method to see if the user entered value is valid as explained here How can I check if a single character appears in a string?

Community
  • 1
  • 1
Nobody
  • 397
  • 1
  • 5
  • 25
1

You could use this:-

if (! ArrayUtils.contains( alpha, "[i-dont-exist]" ) ) {
    try{
        throw new Exception("Not Found !");
    }catch(Exception e){}
}

Documentation Here

Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
1

if the purpose to check the existence of the elements inside collections its more reasonable to use a set since the access time is constanct

so Instead

   Set<String> set = new HashSet<>();//if not java 8 make it HashSet<String>
   set.put("a") // do this for all the strings you would like to check

Then to check if a string exists in this set

if(set.contains(str)) //str is the string you want to make sure it exists in the collections
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

Use this approach if you would like to stick with array instead of other data structures.

  1. Create a method that return true if the user input is present in the array, otherwise return false

    public static boolean isValid(String input) {
        String[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 
            "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", 
            "w", "x", "y", "z"};
    
        for(String s: alpha) {
            if(input.equals(s)) {  //use input.equalsIgnoreCase(s) for case insensitive comparison
                return true;    //user input is valid
            }
        }
    
        return false;   //user input is not valid
    }
    
  2. In the calling method, simply pass the user input to isValid

    //write some codes here to receive user input.....
    
    if(!isValid(input))
        System.out.println("Oops, you have entered invalid input!"); 
    
Jie Heng
  • 196
  • 4