Parth
Parth

Reputation: 265

Have trouble getting an input using Scanner Class

I'm going through my Java text book and for some reason I cannot compile the following code.

import java.util.*; 
public class ComputeAreaWConsoleInput
{

  public static void main (String [] args)
  {
   //Create Scanner Obj
   Scanner sc = New Scanner(System.in);

   //Get Radius
   System.out.print("Please Enter the Radius: ");
   double radius = sc.nextdouble();
   //determine area
   double area = 3.14159 * radius * radius;
   //display results
   System.out.println("The Area of the Circle w/ radius(" + radius +") is: " 
   + area);
  }
}

I am getting the following error:

 /tmp/java_H98cOI/ComputeAreaWConsoleInput.java:8: error: ';' expected
   Scanner sc = New Scanner(System.in);
                   ^
1 error  

What shall be done to compile the code?

Upvotes: 0

Views: 244

Answers (3)

Aditya Singh
Aditya Singh

Reputation: 2443

You've written:

New Scanner(System.in);  

You N in New is capital.

The actual keyword is new and not New.

Solution:

Change your line of code to:

new Scanner(System.in);  

And there is another error.

It should be:

sc.nextDouble();  // with 'D' capital  

and not

sc.nextdouble(); 

Upvotes: 1

Alp
Alp

Reputation: 3105

See my comment: Here is the fixed version of your code:

public static void main (String [] 
{
//Create Scanner Obj
Scanner sc = new Scanner(System.in);

//Get Radius
System.out.print("Please Enter the Radius: ");
double radius = sc.nextDouble();
//determine area
double area = 3.14159 * radius * radius;
//display results
System.out.println("The Area of the Circle w/ radius(" + radius +") is: " + area);
sc.close();  // DO NOT forget this
}

Upvotes: 1

ganeshvjy
ganeshvjy

Reputation: 407

Two changes to your program.

Change New to new.Change the line

Scanner sc = New Scanner(System.in);

to

Scanner sc = new Scanner(System.in);

and other error in the program is scanning a double. Please change double to Double.So change the below line

double radius = sc.nextdouble();

to

double radius = sc.nextDouble();

It should work fine!

Upvotes: 1

Related Questions