Hi,
In my java program, if the user enters the wrong character such as “E” or “A” in my program how can I output a message to the user? The line which I’m speaking about in my program is when the user has to choose to input one out of three characters P, M or D. However, if the user inputs another character which isn’t listed I want the program to display a message.
As you can see if the user inputs “E” the program skips to the next step. However, I want the user to be notified and asked again to enter the correct value which is “P, M or D”.
In order to do what I’m trying to achieve I believe I need to do an if statement so here what I have tried.
if((i = P,M,D) && (i < ?)) {
System.out.println("ERROR, please input only P,M or D.");
}
JAVA CODE
String grade;
int yr2cred[] = new int[18];
char yr2grade[] = new char[18];
for (int i = 1; i <= 18; i++) {
System.out.println("Please enter your grade for unit " + i +" "+(username) + ".");
grade = userinput.next();
yr2grade[i - 1] = grade.charAt(0);
if (yr2grade[i - 1] == 'P' || yr2grade[i - 1] == 'p') {
yr2cred[i - 1] = yr2cred[i - 1] + P;
}
if (yr2grade[i - 1] == 'M' || yr2grade[i - 1] == 'm') {
yr2cred[i - 1] = yr2cred[i - 1] + M;
}
if (yr2grade[i - 1] == 'D' || yr2grade[i - 1] == 'd') {
yr2cred[i - 1] = yr2cred[i - 1] + D;
}
}