Hi Nick,
That is a tricky one given a variable of type String can contain anything from a word to a digit.
To read user input into a String is relatively straightforward however, for example:
import java.util.Scanner;
public class Clothes {
static String clothingItem = "";
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a clothing item");
clothingItem = input.next();
System.out.println("You entered " + clothingItem);
}
}
In you wanted to you could replace clothingItem = input.next() with:
clothingItem = input.nextLine();
In terms of testing whether someone entered a clothing item, you could make a String array to store items you deem to be clothing items, then test the input of user against the array, followed by a second conditional statement for validation. For example:
import java.util.Scanner;
public class Clothes {
static String clothingItem = "";
static String [] clothing = {
"shoes", "socks", "pants", "shorts", "trousers", "jeans",
"dress", "skirt", "stockings", "underwear", "blouse", "shirt",
"singlet", "t-shirt", "skivvy", "jersey", "scarf", "hat"};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = 0;
System.out.println("Enter a clothing item");
clothingItem = input.next();
/* Test whether entered item is clothingItem
* First check Array if item is there making
* sure user input is set to LowerCase to match
* array of lowercase clothing items
*/
for (int i = 0; i < clothing.length; i++) {
if (clothingItem.toLowerCase(
).equals(clothing[i])) {
x = 1;
}
else {
x = 0;
}
}
// Second conditional statement for validation
if (x == 1)
System.out.println("The clothing item you entered was " + clothingItem);
else
System.out.println("Error! You did not enter a clothing item");
}
}
Regards,
Javalad