简体   繁体   中英

how to fix a missing return statement error in java

I'm making a code for a madlibs game and the compiler keeps saying that I am missing a return statement at the very end of the method. How do I fix this?

public static String getNextStep (Scanner scan, String prompt)
   { 
      boolean go = true;    
      System.out.print(prompt);
      String filename;
      if (scan.hasNext())
      {
         while (go)
         {
            filename = scan.next();
            if (((filename.toLowerCase()).equals('c'))||((filename.toLowerCase()).equals('v')))
               return filename;
            else if ((filename.toLowerCase()).equals('q'))
            {
               System.out.print("Thanks for playing!"); 
               go = false;
               return null;
            }
            else 
            {
               System.out.print(prompt);
               return null;
            }
         }
      }
      else 
         return null;
   }

Change that final

else
    return null;

to

return null;

I have not validated your algorithm in any way. But a String will never equal a char . So, this

if (((filename.toLowerCase()).equals('c'))||((filename.toLowerCase()).equals('v')))

should be

if (((filename.toLowerCase()).equals("c"))||((filename.toLowerCase()).equals("v")))

and

else if ((filename.toLowerCase()).equals('q'))

should be

else if ((filename.toLowerCase()).equals("q"))

for the same reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM