Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Scanner class error "java.lang.Error"

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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? Thanks <3


    C.


  2. #2
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Scanner Class Confusion

    Or you could use Scanner (Java Platform SE 6))

    // Json

Similar Threads

  1. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 04:09 AM
  2. Confusion in creating class with OOPS concept
    By grbsmj in forum Object Oriented Programming
    Replies: 3
    Last Post: May 6th, 2009, 04:14 AM
  3. [SOLVED] Problem in Coin-counter with scanner class
    By coccoster in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 25th, 2009, 09:46 AM
  4. Replies: 1
    Last Post: May 13th, 2008, 09:08 AM