Quick JAVA Can not Find symbol?

Quick JAVA Can not Find symbol?
I am working on some code and I cant understand cannot find symbol. I have tried everything. Please help thanks.

import java.util.Scanner;
public class Sales
{

public static void main(String args[]) {
Sales Sales = new Sales();

Sales.CalculateSales();

Scanner Scanner = new Scanner(System.in);

}

public void CalculateSales()

{
int product= 1;

while (product <= 4)
{
if (product ==5) break;
System.out.printf("\nThe value of the product is:");
Scanner.nextInt();
System.out.printf("\nEnter number sold product#:" +product);
product++;

{ double price1;
double price2;
double price3;
double price4;

double value1= 239.99;
double value2= 129.75;
double value3= 99.95;
double value4= 350.89;

double gross;
double earnings;

int numberSold;

if (product==1){
gross= (numberSold * price1) + gross;
}

else {
product=2;
gross= (numberSold * price2) + gross;
}

if (product==3){
gross= (numberSold * price3) + gross;
}
else {
product=4;
gross =(numberSold * price4) +gross;
}

} //end of while loop statement

earnings = (gross *.09) + 200;

System.out.printf("The earnings are %d" );

} //end of main class

Sales.java:28: non-static method nextInt() cannot be referenced from a static context
Scanner.nextInt();
^
Sales.java:67: cannot find symbol
symbol : variable earnings
location: class Sales
earnings = (gross *.09) + 200;
^
Sales.java:67: cannot find symbol
symbol : variable gross
location: class Sales
earnings = (gross *.09) + 200;
^

Best answer:

3 things are wrong that I see right away:

you cannot caller Scanner.nextInt() statically.

This means you need to make an object of class scanner,
i.e., Scanner input = new Scanner(System.in);
input.nextInt();

would be the way to do it.

Also, earnings and gross are both block variables, you created them inside a block, and tried referencing them outside of it.
A block is basically anything inside its own { } braces

EDIT: I see you did try to make a scanner object… but its not in the same method so it will be out of scope (not available in the function your trying to use it in) also, try naming it something other than Scanner, the class name.

Tags: , , ,

Under Forum

Leave a Comment for Quick JAVA Can not Find symbol?

Required

Required, hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Forum