This is a program that allows the user to build a high school schedule. I would like to validate an integer as a high school class number in my String method. Here's the name of my method with the parameters.
public static String addClass(String name, String day, String momentOfDay, int group){
The user has to put an integer as the value of a school class number. The number has to start with a specific number according to the day of class. Here's a table that explains the validation wanted
Day of Class | Valid Group
Monday | The first number of the group must start with 1 ( example 10, 14...)
Tuesday | The first number of the group must start with 2 ( example 20, 22...) ______________________________________________________________________________ Wednesday | The first number of the group must start with 3 ( example 30, 31...) ______________________________________________________________________________ Thursday | The first number of the group must start with 4 ( example 40, 31...) ______________________________________________________________________________ Friday | The first number of the group must start with 5 ( example 50, 56...)
Here's what the output should look like ( the terms in bold are the entered values by the user ) :
********************** ADD CLASS TO SCHEDULE ********************** Name of class : **INF1120** Day of class : **Monday** Moment of the day of the class : **PM** Group of class : **12**
I'm using the scanner to allow the user to enter the wanted integer. I completed the name of class, day and moment of the day part.
However, I'm having a hard time to validate the first number of the group integer according to the days in the table. Here's my code :
import java.util.Scanner;
public class schedule {
public static String addClass(String name, String day, String momentOfDay, int group) {
Scanner keyboard = new Scanner(System.in);
System.out.print("day of class: ");
day = keyboard.nextLine();
if( day != "monday" || day != "tuesday" || day != "wednesday"
|| day != "thursday" || day != "friday" ) {
System.out.print("Error, the day has to be either : monday, tuesday, wednesday, thursday, or friday...");
}
else if(day = "monday" || day = "tuesday" || day = "wednesday"
|| day = "thursday" || day = "friday" ) {
return day;
}
System.out.print("Moment of day: ");
momentOfDay = keyboard.nextLine();
if(momentOfDay != "am" || momentOfDay != "pm" || momentOfDay != "night") {
System.out.print("Error, the moment of the day has to be : am, pm, or evening...");
}
else if(momentOfDay == "am" || momentOfDay == "pm" || momentOfDay == "evening") {
return momentOfDay;
}
System.out.print("Class group");
group = keyboard.nextInt();
while(day == "monday" || day == "tuesday" || day == "wednesday"
|| day == "thursday" || day == "friday"){
if (String.valueOf(Math.abs(int(group)).charAt(0) == 1){
return group;
}
else {
System.out.print("Error, group number is invalid");
}
}
}
}
However, it is not compiling because the return value cannot be an int which is required. Here's the error.
Type mismatch: cannot convert from int to String
It is asking me to either change the return type to int or change the type of group to String.
Should I change the type of group in the parameter ? What did I do wrong ?
I tried to research the methods in this link but can't seem to figure it out.