Rudedog9d
Rudedog9d

Reputation: 71

Simple Java Confusion

I am working on A Java computing course, and I am stumped on what is causing an error here. I tried to research it but i seems difficult to search for this kind of error.

ERROR: source_file.java:12: error: cannot find symbol
double r = scan.nextDouble; ^
symbol: variable nextDouble
location: variable scan of type Scanner

Code:

 import java.io.*;
 import static java.lang.System.*;
 import java.util.Scanner;

 class Rextester{


  public static void main (String str[]) throws IOException {

     Scanner scan = new Scanner(System.in);
     System.out.println("Radius:");
     double r = scan.nextDouble;
     double circumference = (2 * 3.14 * r);
     double area = (r * r * 3.14);
     System.out.println("Circumference: " + circumference );
     System.out.println("Area :" + area );

    }

}

Upvotes: 0

Views: 221

Answers (6)

Stefan
Stefan

Reputation: 2613

ERROR: source_file.java:12: error: cannot find symbol double r = scan.nextDouble;

this error means that you try to access a public element of the object that is hold in the variable scan.

What you want to call is not a public variable but the method nextDouble()

Therefor you must use scan.nextDouble(); to call the method.

Upvotes: 3

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

nextDouble() is a method not property

so try:

double r = scan.nextDouble();

try to shorten your code:

  public static void main (String str[]) throws IOException {

     Scanner scan = new Scanner(System.in);
     double r = scan.nextDouble();
     System.out.println("Circumference: " + (2 * Math.PI * r));
     System.out.println("Area :" + (r * r * Math.PI));

    }

Upvotes: 0

AlexR
AlexR

Reputation: 2408

Little side note, apart from

scan.nextDouble();

you should also use

Math.PI

instead of 3.14


Fixed code:

import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;

class Rextester{


    public static void main (String str[]) throws IOException {

        Scanner scan = new Scanner(System.in);
        System.out.println("Radius:");
        double r = scan.nextDouble();
        double circumference = (2 * Math.PI * r);
        double area = (r * r * Math.PI);
        System.out.println("Circumference: " + circumference );
        System.out.println("Area :" + area );

    }
}

Upvotes: 0

Balaji Krishnan
Balaji Krishnan

Reputation: 1017

change your line to double r = scan.nextDouble(); instead of double r = scan.nextDouble

Upvotes: 1

Prasad Kharkar
Prasad Kharkar

Reputation: 13566

change scan.nextDouble to scan.nextDouble(). It is a method and not field. So it requires brackets.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159864

Methods require parenthesis

double r = scan.nextDouble();
                          ^

Upvotes: 9

Related Questions