wood burning stoves
The moose likes Beginning Java and the fly likes help with easy code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with easy code" Watch "help with easy code" New topic

help with easy code

Colm Flaherty

Joined: Aug 28, 2011
Posts: 1
Prog1.java:4: cannot find symbol
symbol : variable sc
location: class Prog1
double f = sc.nextDouble();
Prog1.java:6: cannot find symbol
symbol : variable sc
location: class Prog1
double g = sc.nextDouble();

That is the error i receive when i type in the following code

public class Prog1 {
public static void main(String[] args) {
System.out.println("Please Enter Miles");
double f = sc.nextDouble();
System.out.println("Please Enter Gallons");
double g = sc.nextDouble();
System.out.println("Miles per gallon is:");

so what am i doing wrong?
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
The compiler cannot find the "sc" because you haven't declared it. So before taking the input from user you have to write this and then you can use and yes to use the Scanner class we have to import the util package, so do this
at the top of your code
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

if the compiler says it cannot find symbol, the first thing I would suggest is looking for the symbol yourself. cntl-f is fabulous for that task. If you do that, you will see that you have never told the compiler what this 'sc' is.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
. . . and welcome to the Ranch
I agree. Here's the link: http://aspose.com/file-tools
subject: help with easy code
It's not a secret anymore!