Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Cannot find symbol error?

Tags:

java

math

I am trying to compute the total price given the unit price, 17, and the number of items, 20.

public class hw1_task3 {
    public static void main(String[] args) {
        int total = units * price;
        int units = 20;
        int price =  17;
        System.out.printf("The total is: %d", total);
    }    
}

What is wrong with the program? I keep getting an error about not being able to find the symbols. I am very new to java, any help is greatly appreciated.

like image 514
Seth Johnson Avatar asked Dec 07 '25 23:12

Seth Johnson


2 Answers

You need to move units and price above total like this:

int price =  17;
int units = 20;
int total = units * price;
like image 90
jheimbouch Avatar answered Dec 09 '25 15:12

jheimbouch


You are using the variable before the declaration. These lines

int units = 20;
int price =  17;

should be written first and

int total = units * price;

after that.
So the correct lines will be:

int units = 20;
int price =  17;
int total = units * price;
like image 40
Mohammad Faisal Avatar answered Dec 09 '25 13:12

Mohammad Faisal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!