Scanner class error "java.lang.Error"
I've just started studying Java, and I really don't know how to correct this error. It keeps saying :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator * is undefined for the argument type(s) double, String
at CircleScanner.main(CircleScanner.java:16)
This is what I have, so far :
import java.util.Scanner;
public class CircleScanner
//Program for the area of a circle with the user giving radius input
{
public static void main (String[] args)
{
String message;
Scanner scan = new Scanner (System.in);
System.out.println("Please enter a value for the radius of the circle:");
final double PI = 3.14159;
message = scan.nextLine();
double area = PI * message * message;
System.out.println("The area of this circle with radius " + message + " is " + area + "");
}
}
Can anyone point out what the problem is, and how to rectify these errors? :D Thanks <3
C.
Re: Scanner Class Confusion
Your area is of type double, but you are multiplying PI and message (which is a String). There are two things you can do:
1. Change String message into double message, and instead of message = scan.nextLine(), it should be message = scan.nextDouble()
2. You can change the string into a double.
message = scan.nextLine();
double x = Double.parseDouble(message); //this converts the String message into type double
double area = PI * x * x;
Good luck.
Re: Scanner Class Confusion