Reputation: 107
The other Chapter 2 example files are working fine. I can't figure out why this particular class is having these problems - where I'm getting errors are labeled in comments.
package chapter2;
public class DataTypeConversion {
public static void main(String[] args) {
double x;
int pies = 10; //error: not a statement
x = y; //error: cannot find symbol: variable y
int pies = 10, people = 4;
double piesPerPerson;
piesPerPerson = pies / people;
piesPerPerson = (double) pies / people;
final double INTEREST_RATE = 0.069; //Note that the variable name does not have
amount = balance * 0.069; //error: cannot find symbol: variable: amount
amount = balance * INTEREST_RATE;
}
}
My goal is to use this code as a stand alone Java file, so I don't know why it's complaining so much. Any ideas?
Upvotes: 0
Views: 139
Reputation: 9405
Duplicate variable declaration:
int pies = 10;
and
int pies = 10, people = 4;
Upvotes: 0
Reputation: 86
y
is not declared or initialized before use. eg: int y = 0;
(note, y
is supposed to be an int
, due to the exercise demonstrating narrowing/widening concepts)pies
is declared twice, lines 30 and 41. Remove line 30.amount
is not declared . eg: double amount = balance * 0.069;
balance
is not declared or initialized before use, eg: double balance = 10.0;
(must be done
before attempting to use it with amount
in line 59)I think the key you need to remember at this stage is that before you can use a variable for the first time, it must be declared as a specific data type. eg: int
, double
, String
, etc. A good practice, particularly as a student (which I am), is to declare all of your variables at the beginning of the code block (class/method/function, etc) in which they are declared.
Upvotes: 1
Reputation: 347314
I'm not sure what y
is suppose to be equal, but you've not decalred it anywhere, so Java doesn't know anything about it...
You could try something like...
double x, y, amount, balance; // Might as weel add amount and balance cause they'll cause you errors now...
int pies = 10;//error: not a statement
x = y; // But this is garbage as y's value is undefined
Upvotes: 0
Reputation: 13986
You must declare your variables before using them. Add this line at the top:
double y, amount, balance;
Upvotes: 3