Keith MacInnis Keith MacInnis -4 years ago 47
Java Question

Exact Syntax for passing char array (typed out in a method parameter)

doPrompt({'a','t','q'},"Test");


I get a illegal start of expression error starting with the
{
character.

Here is the method being called:

public static char doPrompt(char[] validCharacterValues, String prompt) {
do {
System.out.print(prompt);
Scanner keyboardMonster = new Scanner(System.in);
String usersInput = keyboardMonster.nextLine().toLowerCase();
if (arrayContains(validCharacterValues,usersInput.charAt(0)))
return usersInput.charAt(0);
} while (true);
}

Answer Source

Arrays can be created inline with new type[]{} with type being the type of the array. So for your case, it would be doPrompt(new char[]{'a','t','q'},"Test");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download