0

I have come across this error a few times, and I try many things to fix it, but I can't seem to find out what is going wrong. here is the code:

import java.util.Scanner;
public class InputExample {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String name1 = scan.nextLine();
        String name2 = scan.nextLine();
        int num1 = scan.nextLine();
        double num2 = scan.nextLine();

        System.out.println("Employee Name:" + name2 + name1);
        System.out.println("Age:" + num1);
        System.out.println("Salary:" + (double)num2);
    }
}

And the error that shows up specicfically is:

[File: /InputExample.java Line: 9, Column: 25] incompatible types required: int found: java.lang.String

[File: /InputExample.java Line: 10, Column: 28] incompatible types required: double found: java.lang.String

1
  • It's because scan.nextLine(); returns a String and you are trying to assign that returned String to an int. You need scan.nextInt(); I think. Commented May 20, 2015 at 16:27

1 Answer 1

3

You're reading in a String, but want to save it as Integer/Double. Try this:

int num1 = Integer.valueOf(scan.nextLine());

Same goes for Double.

Or as OldCurmudgeon mentioned:

int num1 = scan.nextInt();
Sign up to request clarification or add additional context in comments.

2 Comments

scan.nextInt(); may be better.
You can change it in your answer - considered good practice to make your answer the best you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.