1

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.

ForzaTekk
  • 33
  • 5
  • `must start with 1 ( example 20, 22...)` ... um... are you sure you don't mean "must start with a 2"? – markspace Mar 14 '20 at 17:03
  • What is the type of `group`? If it's an `int` it doesn't support `.charAt()`. Maybe convert the int to a string first? – markspace Mar 14 '20 at 17:06
  • Whenever you're posting a question here, if there's an error message, COPY the error message and paste it into your question. Java gives great error messages at both compile and run time, in general, and in order to help you we need to know what it says. – arcy Mar 14 '20 at 17:19
  • @markspace group is an integer – ForzaTekk Mar 14 '20 at 18:03
  • You have a compilation error at line 192 of file `addClass,java`. Can you post the contents of file `addClass.java` ? – Abra Mar 14 '20 at 18:43
  • @Abra It is compiled in french. There's an error on line 192 which is the main because the method addClass is not compiling because of the problem I'm having. I called the method on the main – ForzaTekk Mar 14 '20 at 18:49
  • It sounds like you just want to know what the first number of the user-typed int is, so that you can then do whatever validation you need to do. That's been asked on SO before (many times, in fact), with each one getting closed as duplicate of "[Return first digit of an integer](https://stackoverflow.com/questions/2051817/return-first-digit-of-an-integer)", so... this is probably a duplicate too? – Mike 'Pomax' Kamermans Mar 14 '20 at 18:50
  • @Mike'Pomax'Kamermans should the return value be a string instead of an integer ? I declared it as an integer in my method, but the compiler is not allowing me to return it Did i do something wrong ? – ForzaTekk Mar 14 '20 at 19:08
  • No idea: you're not showing a full method, so there's literally no way to tell what you should be returning based on the type you defined as being the return type. – Mike 'Pomax' Kamermans Mar 14 '20 at 19:44
  • @Mike'Pomax'Kamermans added the whole method. – ForzaTekk Mar 14 '20 at 20:35
  • Thank you - could I ask you to please format and intent it properly, though? It's kind of haphazard at the moment. (if you use an IDE, you should be able to just tell it to auto-format your code, which is a function you should definitely over-rely on =) – Mike 'Pomax' Kamermans Mar 14 '20 at 20:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209642/discussion-between-forzatekk-and-mike-pomax-kamermans). – ForzaTekk Mar 14 '20 at 20:59

1 Answers1

-1

When I copied the code of class schedule that you posted, I got a compilation error for this line:

if (String.valueOf(Math.abs(int(group)).charAt(0) == 1){

The int should be removed. group is a parameter of method addClass() and it is an int. So the line of code should be:

if (String.valueOf(Math.abs(group)).charAt(0) == 1){

After correcting that line, the line following it causes a compilation error. That line is:

return group;

Method addClass() is declared to return a String but group is an int. So that line should be changed to:

return String.valueOf(group);

After correcting that line of code, I got yet another compilation error, namely that the method does not return a value. This is the while loop in method addClass():

while (day == "monday" || day == "tuesday" || day == "wednesday" || day == "thursday"
            || day == "friday") {

If day is "sunday", the loop is terminated. There needs to be a return statement after that while loop.

But even after fixing all the compilation errors, your code will not work due to logic errors. I just showed you one of those errors in the while loop. Similarly this if statement will not do what you want.

if (day != "monday" || day != "tuesday" || day != "wednesday" || day != "thursday"
            || day != "friday")

Besides that fact that you need to use method equals() to check if two strings are equal, if day is "tuesday", then the first condition, namely day != "monday" is true. If you want to ensure that the user enters a valid day, then you need the following:

if ("monday".equals(day) ||
    "tuesday".equals(day) ||
    "wednesday".equals(day) ||
    "thursday".equals(day) ||
    "friday".equals(day)) {
    return day;
}
else {
    System.out.print(
                    "Error, the day has to be either : monday, tuesday, wednesday, thursday, or friday...");
}

If day and momentOfDay and group are all parameters of method addClass(), why do you ask the user to enter these values inside method addClass() ? Also the other method parameter, name, is never used in the method.

I think you need to create three, separate methods. One to get the day from the user, another to get the momentOfDay and yet another to get the group.

I suggest you read the book Java by Comparison by Simon Harrer, Jörg Lenhard, Linus Dietz

Abra
  • 19,142
  • 7
  • 29
  • 41
  • the `day` and `momentOfDay` and `group` must be the parameters from the same method. And the method must stock a variable from type String. I cannot create 3 separate methods. – ForzaTekk Mar 16 '20 at 22:24