-3

I have been asked to prompt the user for a string data and then validate the user input against an array of the data containing the values that the user should enter and prompt the user is the string is not valid.

 public static void main(String[] args)
 {
    Scanner keyboard = new Scanner(System.in);

    //List of values the user suppose to enter

   String[] list = {"Monday","Thusday", "Wednesday", "Thusday", 
                   "Friday", "Saturday", "Sunday"}


     System.out.println("Enter a day:"\n Eg: Monday or Thursday")
     String day = keyboard.next();

 /**
   What should I write so that I can validate the user input against an
   string array of days? And prompt the user again if his input is not 
   a day.

 */

}
Gery .Z
  • 1
  • 2
  • 2
    We are a question-and-answer site, not a coders-for-hire service. Please narrow your question down to a specific coding question that would be on-topic for this site. See: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) and [How to ask a good question when I'm not sure what I'm looking for?](https://meta.stackoverflow.com/questions/262527/how-to-ask-a-good-question-when-im-not-sure-what-im-looking-for) – Joe C Jan 20 '18 at 22:05
  • (a) Always search Stack Overflow before posting. You can assume basic questions such as this have already been asked and answered. (b) By the way, see [`DayOfWeek`](https://docs.oracle.com/javase/9/docs/api/java/time/DayOfWeek.html) enum. – Basil Bourque Jan 20 '18 at 23:54

2 Answers2

0

This should work

public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    //List of values the user suppose to enter

    String[] list = {"Monday","Thusday", "Wednesday", "Thusday", 
            "Friday", "Saturday", "Sunday"};

    System.out.println("Enter a day:");
    String day = keyboard.next();

    if(!Arrays.asList(list).contains(day)) {
        System.out.println("Input is not a day!");
    }
}
0

You can loop through the Array using a for loop (or a do/while loop, or a while loop) then compare the User's input with that which is contained within each array element to see if there is a match.

You can even take it to a point where.... What if the User enters monday instead of Monday? Or what if the user enters mon or Mon instead of Monday? You can handle these situations (if desired) during the looping of the Array (within a for loop code block for example).

In the case of where a User enters monday instead of Monday, read how to utilize Java's String.toLowerCase(), String.toUppercase(), and or the String.equalsIgnoreCase() methods.

In the case of where a User enters Mon instead of Monday, read how to utilize Java's String.length() and String.substring() methods.

You could of course have these particular situations contained within your String Array for example:

{mon, Mon, monday, Monday, MONDAY, tue, Tue, tuesday, Tuesday, TUESDAY, ...etc...}

but then, that's no fun and is more prone to errors (tUesDay). You never know how a User will enter a string, even if you provide an example but then again you can simply refuse to accept their input unless they follow the rules!

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22