I am trying to get an element from array and use it later in my code, but I have some logic error in my code. I would like a user to enter what day he or she needs cleaning and then to print out the day of cleaning. Here is the code:
import java.util.Scanner;
public class arrayLoopTest {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int i = 0;
while (true) {
System.out.println("On what day should the cleaning be scheduled ? ");
String schedule = scr.nextLine();
for (i = 0; i < days.length; i++) {
if (days[i].equals(schedule)) {
System.out.println("Cleaning scheduled for " + schedule);
} else {
System.out.println("Invalid, You have to choose one of these days: \n " + days[i]);
}
}
}
}
}
Console Output
On what day should the cleaning be scheduled ?
Thursday
Invalid, You have to choose one of these days:
Monday
Invalid, You have to choose one of these days:
Tuesday
Invalid, You have to choose one of these days:
Wednesday
Cleaning scheduled for Thursday
Invalid, You have to choose one of these days:
Friday
Invalid, You have to choose one of these days:
Saturday
Invalid, You have to choose one of these days:
Sunday
On what day should the cleaning be scheduled ?