incompatible types: void cannot be converted to int

D_Wagner

I'm extremely new when to comes to Java and programming in general. I am trying to create a simple program where you guess my age and if you are right it will say "correct" and if you are wrong it will say "wrong".

This is my code:

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

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



        int validInput = 20;
        if (validInput == 20) {
            return System.out.println("Correct!!");
        }
        else {
            return System.out.println("Wrong....");
        }
    }
}

I get the error "incompatible types: void cannot be converted to int" but I have no void class in the code? I know my code is probably awful but if you guys could point me in the right direction that would be great. Thanks.

GermaineJason

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....");
    }
}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java incompatible types: int cannot be converted to int[]

From Dev

Java incompatible types: int cannot be converted to int[]

From Dev

error: incompatible types: int[][] cannot be converted to int

From Java

incompatible types: int cannot be converted to T

From Java

Java : incompatible types; int cannot be converted to string

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

Java: toString error "error: incompatible types: int cannot be converted to String"

From Dev

app setBackground() error: incompatible types: int cannot be converted to Drawable

From Dev

Incompatible types : Int cannot be converted to "class" data type

From Dev

JAVA: Incompatible types: int cannot be converted to java.lang.String

From Dev

incompatible types: Node cannot be converted to Tab

From Dev

incompatible types: HomeFragment cannot be converted to Fragment in Android

From Dev

Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

From Dev

Error: incompatible types: double cannot be converted to JTextField

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

Incompatible types: Object cannot be converted to ParseObject

From Dev

incompatible types: char[] cannot be converted to CharSequence

From Dev

error: incompatible types: MainFragment cannot be converted to Activity

From Dev

incompatible types: FragmentDark cannot be converted to Fragment in Android

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

incompatible types: Object cannot be converted to CoreLabel

From Dev

Java JComboBox Incompatible Types: Cannot be converted to string

From Dev

error: incompatible types: Object cannot be converted to MyClass

From Dev

incompatible types: String[] cannot be converted to String

From Dev

incompatible types: MainActivity cannot be converted to LifecycleOwner

From Dev

incompatible types - int cannot be converted in to java.lang.string -java - bluej

From Dev

Java - Getting compile error incompatible types int can't be converted to [][]

From Dev

Error:(124, 62) error: incompatible types: Class cannot be converted to Context

Related Related

  1. 1

    Java incompatible types: int cannot be converted to int[]

  2. 2

    Java incompatible types: int cannot be converted to int[]

  3. 3

    error: incompatible types: int[][] cannot be converted to int

  4. 4

    incompatible types: int cannot be converted to T

  5. 5

    Java : incompatible types; int cannot be converted to string

  6. 6

    incompatible types: BigInteger cannot be converted to int

  7. 7

    incompatible types: BigInteger cannot be converted to int

  8. 8

    Java: toString error "error: incompatible types: int cannot be converted to String"

  9. 9

    app setBackground() error: incompatible types: int cannot be converted to Drawable

  10. 10

    Incompatible types : Int cannot be converted to "class" data type

  11. 11

    JAVA: Incompatible types: int cannot be converted to java.lang.String

  12. 12

    incompatible types: Node cannot be converted to Tab

  13. 13

    incompatible types: HomeFragment cannot be converted to Fragment in Android

  14. 14

    Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

  15. 15

    Error: incompatible types: double cannot be converted to JTextField

  16. 16

    Error: incompatible types: Object cannot be converted to char

  17. 17

    Incompatible types: Object cannot be converted to ParseObject

  18. 18

    incompatible types: char[] cannot be converted to CharSequence

  19. 19

    error: incompatible types: MainFragment cannot be converted to Activity

  20. 20

    incompatible types: FragmentDark cannot be converted to Fragment in Android

  21. 21

    Error: incompatible types: Object cannot be converted to char

  22. 22

    incompatible types: Object cannot be converted to CoreLabel

  23. 23

    Java JComboBox Incompatible Types: Cannot be converted to string

  24. 24

    error: incompatible types: Object cannot be converted to MyClass

  25. 25

    incompatible types: String[] cannot be converted to String

  26. 26

    incompatible types: MainActivity cannot be converted to LifecycleOwner

  27. 27

    incompatible types - int cannot be converted in to java.lang.string -java - bluej

  28. 28

    Java - Getting compile error incompatible types int can't be converted to [][]

  29. 29

    Error:(124, 62) error: incompatible types: Class cannot be converted to Context

HotTag

Archive