Question: JAVA Question - Input Validation on String I am trying complete string input validation. I would like it so that the user can only input

JAVA Question - Input Validation on String

I am trying complete string input validation. I would like it so that the user can only input the words "First", "Second" and "Third" strictly, however, currently, they can enter the words and any combination there of; for example, "Fecrd" is accepted because it falls within the "hasNext" group. I was working with a ".matches" validation but deleted it on accident, but I am not sure if that would work and if you can use a series of input options with ".matches".

My sample code is below.

Thank you!

So what I want to do is make sure the user can only and strictly enter either "First", "Second" or "Third" not all at once and not a combination there of, strictly allowing for one of those words only (case does not matter). Please try to use simple techniques I am a noob and am learning.

Sample Code

public static String getString(String message) { System.out.println(message); Scanner sc = new Scanner(System.in); if(sc.hasNext("[First, Second, Third]*")){ place = sc.nextLine(); // input check will print if correct input format System.out.println("Player is " + place+ " place."); } else { System.out.println("Please write out the appropriate place"); // has code to route back to call string } return place;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!