Scanner input = new Scanner(System.in);
String[] colors = {"R", "G", "B", "P", "O", "Y"};
String guess = "";
String valid = false;
//Get Input
guess = input.nextLine().toUpperCase();
// Change String Array to Regular String
String Colors = "";
for(int i=0; i<colors.length; i++) {
Colors += colors[i];
}
// See if Guess Consists out of Correct Letters
if(guess.matches(Colors)) {
valid = true;
}
Basically what I want to achieve with this code is to check if the input given meets the requirements.
In this case does guess
only consists of the letters given in colors
, for example:
if guess = RGB
then valid = true
if guess = RGBA
then valid = false
I know I can also put it in manually like this:
if(guess.matches("[RGBPOY]+")) {
valid = true;
}
But I want to keep it procedural.
Is there a way to get this working? Or am I better off using a different method like say a for loop?
I only recently started with JAVA and am not yet familiar with all the different approaches it has to offer.