How to check if a String is inside of an array of String? (JAVA)
The user should be able to enter a colour and then the program should check if the inputted colour is inside the array or not.
For example:
USER INPUTS: black
The program checks if the colour black is inside an array of colours
PROGRAM OUTPUTS: black
else
PRORGAM OUTPUTS: Sorry mate, black isn't recognised.
commands[] is a parametrized array. If the user types "circle colour black", then circle gets stored in array location [0], colour gets stored in array location [1] and black gets stored in location [2] and so on...
private String[] colours = {"black", "blue", "green", "magenta", "red", "white", "yellow"};
/**
* Checks if the typed in colour is valid.
*/
public void checkColour(String[] commands)
{
String varifiedColour = null;
if(commands[1].equals("colour")){
for(int i = 0; i < colours.length; i++){
if(commands[2].contains(colours[i])){
varifiedColour = colours[i];
//Prints varifiedColour to check if this method works.
System.out.println(varifiedColour);
}
}
}
}