Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with easy code

 
Colm Flaherty
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic