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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started