Domanda

I need it to be an int to return but it keeps saying

----jGRASP exec: javac -g CSCD210Lab8Functions.java

CSCD210Lab8Functions.java:118: incompatible types
found   : int
required: java.lang.String
      return finalExam;





 public static String readFinalScore(Scanner kb)
   {int finalExam;
      do{
         kb.nextLine();
         System.out.print("Enter the score of the final-->");
         finalExam = kb.nextInt();
         kb.nextLine();
         if (finalExam<0)
         {System.out.print("Invalid answer please try again " );}
      }while(finalExam<0);
      return finalExam;

i casted it as an int and then kb.nextInt not sure why it's freaking out

È stato utile?

Soluzione

public static String readFinalScore(Scanner kb) // return type String
{int finalExam; // finalExam is int
...
return finalExam;   // returning int value when it expects String

return type is String and finalExam is int.

Change to

public static int readFinalScore(Scanner kb)

Altri suggerimenti

You're returning an int when it wants a String

Change:

public static String readFinalScore(Scanner kb)

Into:

public static int readFinalScore(Scanner kb)

If you meant to declared your method to return String, java won't automatically convert an int to a String.

Change your return statement to:

return String.valueOf(finalExam);

Or you can "cheat" too:

return finalExam + "";

However, if you really want to return an int, then declare your method like this:

public static int readFinalScore(Scanner kb)

Your current method code will then work.

Your return statement do not match..

So you can use Integer.toString(finalExam) to return String type

Hope it helps..

Your return declaration is incorrect. you should change to int instead of String.

public static int readFinalScore(Scanner kb)

   return finalExam ;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top