incompatible types: void cannot be converted to int [duplicate]

Your program does not have to return an int in public static int main. Instead you can have it as void (meaning don’t return anything). You should simply just print your statements and don’t return them. Also the int[] should be String[] and Scanner should check for nextInt() as pointed out in comments!

import java.util.InputMismatchException;
import java.util.Scanner; // This will import just the Scanner class.

public class GuessAge {
public static void main(String[] args) {
   System.out.println("\nWhat is David's Age?");
   Scanner userInputScanner = new Scanner(System.in);
   int age = userInputScanner.nextInt();



    int validInput = 20;

    // typo in your code - compare to age
    if (validInput == age) {
        System.out.println("Correct!!");
    }
    else {
        System.out.println("Wrong....");
    }
}

}

Leave a Comment