-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.

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 Commented Apr 19, 2017 at 13:25
  • 2
    !input.matches("[a-z]") should work... Forget the array of the alphabet Commented Apr 19, 2017 at 13:27

5 Answers 5

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().

Sign up to request clarification or add additional context in comments.

4 Comments

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!
Can you add an example?
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");
if(!list.contains(guess)) should work as long as guess is a String.
1

You could use this:-

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

Documentation Here

Comments

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 

Comments

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?

Comments

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!"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.