-6

How can I fix this problem here:

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

My code:

import jdk.internal.util.xml.Input;

import java.util.*;    
public class HelloWorld { //this is a comment
    public static void main(String[], args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome");
        int grade = input.nextLine();

        if (grade > 60) {
             System.out.println("Admited");
        }
    }
}

It says it requires an integer, but it found a string.

Trooper Z
  • 1,617
  • 14
  • 31
  • You didn't actually show your code... – Steve11235 Aug 23 '18 at 20:33
  • 1
    Related: _[What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java)_ – Luca Kiebel Aug 23 '18 at 20:34
  • 2
    Scanner.nextLine() returns a String, which cannot be type casted to an int primitive. https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – Fullstack Guy Aug 23 '18 at 20:35
  • 1
    Please include all code as formatted text within the question itself, not at images or links to an external code sharing site. – jmoerdyk Aug 23 '18 at 20:38
  • Possible duplicate of [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – fabian Aug 24 '18 at 00:32

1 Answers1

1

String is an Object, int is a primitive; they are not compatible. Use Integer.parseInt(String, 10); The "radix" will stop certain code analyzers from complaining. Note that a String that can't be parsed will result in an exception.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
  • *The "radix" will stop certain code analyzers from complaining.* About what? – shmosel Aug 23 '18 at 20:43
  • Apparently, some code analyzers think that using the default radix is ambiguous or some such. I was annoyed the first time it happened. – Steve11235 Aug 24 '18 at 14:25