-2

I am trying to return an integer if particular string is true. (For this I do not want to use an array).

   public String calcNextDay()
  {
     if (day == "Sunday"){
      return 0; // If day is Sunday return 0
     }else if (day == "Monday"){
      return 1; // If Day is Monday return 1
     }else if (day == "Tuesday"){
       return 2; // If day is tuesday return 2
     }else if(day == "Wednesday"){
       return 3; // If day is Wednesday return 3
     }else if(day == "Thursday"){
        return 4;// If day is Thursday return 4
     }else if (day == "Friday"){
         return 5;// If day is Friday return 5
     }else if(day == "Saturday"){
        return 6;// if day is Saturday return 6
          }
          }

I have also tried this, but am getting an error with the return dayValue stating it can not be converted to String (even though I do not want to turn it to a string)

   public String calcNextDay()
  {
     int dayValue = 0;
     if (day == "Sunday"){
     dayValue = 0; // If day is Sunday return 0
     }else if (day == "Monday"){
     dayValue =1; // If Day is Monday return 1
     }
     return dayValue;
     }

What am I doing wrong?

Based on the comments I changed some code to

Public String calcNextDay()
  {
     int dayValue = 0;
     if (day.equals("Sunday")){
        dayValue = 0;
     }else if (day.equals("Monday")){
        dayValue = 1;
     }else if (day.equals("Tuesday")){
        dayValue = 2;

     }return dayValue;
          }

error: incompatible types: int cannot be converted to String }return dayValue;

Thank you, I got passed that problem and then I get to the error Day.java:178: error: non-static variable dayValue cannot be referenced from a static context System.out.println("Your day is stored as " + testday.setDay() + dayValue); ^ 1 error

My main() is a static, but my dayValue is not

  • you are returning `dayValue` which is an int, however `calcNextDay()` is declared as returning a `String`. Try this: `return Integer.toString(dayValue)`, or change the declaration to `public int calcNextDay()` – Bull Aug 19 '14 at 04:29
  • I don't think this is a duplicate per se, but we don't know the type of `day` - from your errors, it leads me to believe that it's an `int` and not a `String`. – Makoto Aug 19 '14 at 05:07

2 Answers2

1

All the string comparisons are wrong, in Java they must be done like this:

if (day.equals("Sunday"))

In other words, use equals() for testing equality, instead of ==. Even better, it's a good practice to put the literal value first, in case the other value is null. This is what I mean:

if ("Sunday".equals(day))

And also, you are trying to return an int inside a function that specifies the return type as String, so change this:

   public String calcNextDay()

… to this:

   public int calcNextDay()
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • OK I changed it to that public String calcNextDay() { int dayValue = 0; if (day.equals("Sunday")){ dayValue = 0; }else if (day.equals("Monday")){ dayValue = 1; }else if (day.equals("Tuesday")){ dayValue = 2; }return dayValue; } But I get error: incompatible types: int cannot be converted to String }return dayValue; – Michael Letendre Aug 19 '14 at 03:12
  • 1
    @MichaelLetendre as stated above: you _must_ change the method's return type to `int`, a number is not a `String`! – Óscar López Aug 19 '14 at 03:18
  • Thanks, I just found that, I did not see that you edited the answer by thanks I just found that and it helped – Michael Letendre Aug 19 '14 at 03:40
0

In Java the strings are not compared with ==. The correct way is to use method equals.

if ("Sunday".equals(day))

For safety compare using "Yoda convention": the comparison will not fail even if day is null.

As for returning integer, using a lot of if else if not the best practice either. There are better ways to do it:

  • Use switch statement switch (day) { case "Monday" : return 0; case "Tuseday": return 1; ... }
  • Create an array with day names and search for specific day in array. Return an index of the item found

Also, since you're returning int, your function's result type should be also int

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60