This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with easy code" Watch "help with easy code" New topic
Author

help with easy code

Colm Flaherty
Greenhorn

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:");
System.out.println((f/g));
}
}

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
Bartender

Joined: Oct 02, 2003
Posts: 11883
    
  18

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
Sheriff

Joined: Oct 13, 2005
Posts: 44610
    
  34
. . . 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!