C.Bell C.Bell - 5 months ago 15
Java Question

I am trying to add a String to a Method but an error is returned, what am I missing?

I am fairly new to programming. . .

I am getting an error with the following code :

public class Main {

public static void main(String[] args) {

int highScore = calculateScore(true, 800, 5, 100);
System.out.println("Your final score was " + highScore);


highScore = calculateScore(true, 10000, 8, 200);
System.out.println("Your final score was " + highScore);

displayHighScore(Jack, 3);
}

public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {
if (gameOver) {
int finalScore = score + (levelCompleted * bonus);
finalScore += 2000;
return finalScore;
}
return -1;
}

public static void displayHighScore(String playersName, int position){
System.out.println(playersName + " managed to get to position " + position);
} }

Answer

we dont have enough information but inspecting the error msg

Error:(14, 26) java: incompatible types: int cannot be converted to java.lang.String

it looks like the problem is about HOW you are calling the method you wrote...

you are passing an integer where a string must be passed...

look at the signature and give as parameter exactly what the method needs...

a String and an int

public static void main(String[] args) {
    displayHighScore(2, 1);  // for example this is a wrong paramerer error
    displayHighScore("C. Bryan", 1);  // this is ok!

}

public static void displayHighScore(String playersName, int position) {
    System.out.println(playersName + " managed to get to position " + position);

}