java - If string == "sting text" return int -


this question has answer here:

i trying return integer if particular string true. (for not want use array).

   public string calcnextday()   {      if (day == "sunday"){       return 0; // if day sunday return 0      }else if (day == "monday"){       return 1; // if day monday return 1      }else if (day == "tuesday"){        return 2; // if day tuesday return 2      }else if(day == "wednesday"){        return 3; // if day wednesday return 3      }else if(day == "thursday"){         return 4;// if day thursday return 4      }else if (day == "friday"){          return 5;// if day friday return 5      }else if(day == "saturday"){         return 6;// if day saturday return 6           }           } 

i have tried this, getting error return dayvalue stating can not converted string (even though not want turn string)

   public string calcnextday()   {      int dayvalue = 0;      if (day == "sunday"){      dayvalue = 0; // if day sunday return 0      }else if (day == "monday"){      dayvalue =1; // if day monday return 1      }      return dayvalue;      } 

what doing wrong?

based on comments changed code

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 converted string }return dayvalue;

thank you, got passed problem , error day.java:178: error: non-static variable dayvalue cannot referenced static context system.out.println("your day stored " + testday.setday() + dayvalue); ^ 1 error

my main() static, dayvalue not

all string comparisons wrong, in java must done this:

if (day.equals("sunday")) 

in other words, use equals() testing equality, instead of ==. better, it's practice put literal value first, in case other value null. mean:

if ("sunday".equals(day)) 

and also, trying return int inside function specifies return type string, change this:

   public string calcnextday() 

… this:

   public int calcnextday() 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -