Bank Balance - while loop - wont compile
Code Java:
// Practical 8 - Q7
// Marcus Ward
// 07/11/2011
/* Program to allow user to enter amount they wish to deposit.
Program will continue until user says 'No'. The current bank balance is €500.00 */
import java.util.Scanner;
public class BankBalanceLoop
{
public static void main(String[] args)
{
Scanner keyboardIn = new Scanner(System.in);
double balance = 500.00, newBalance, finalBalance, deposit; // declare variables
char response = 'Y';
while (response == 'Y')
{
System.out.println("Please enter the amount of your deposit: "); // get user input(to be repeated)
deposit = keyboardIn.nextDouble();
System.out.println("Your new balance is" + newBalance);
newBalance = keyboardIn.nextDouble();
System.out.println("Do you want to make another balance ( Y or N)?");
response = keyboardIn.next().charAt(0);
}
finalBalance = balance + deposit;
System.out.println("Your final balance is" + finalBalance);
finalBalance = keyboardIn.nextDouble();
}
}
Error
BankBalanceLoop.java:16: cannot find symbol
symbol : variable Y
location: class BankBalanceLoop
char response = Y;
^
1 error
Re: Bank Balance - while loop - wont compile
The compiler can not find a definition for the variable: Y.
Is Y a variable or did you forget the 's that would make it a character literal?
I see that you have edited the post and changed the source.
Does the program compile OK now?
Re: Bank Balance - while loop - wont compile
Yeah I edited it so it's declared as 'Y' , but still not compiling.
These are the 2 errors that I'm getting now:
BankBalanceLoop.java:22: variable newBalance might not have been initialized
System.out.println("Your new balance is" + newBalance);
^
BankBalanceLoop.java:28: variable deposit might not have been initialized
finalBalance = balance + deposit;
^
Re: Bank Balance - while loop - wont compile
Quote:
variable newBalance might not have been initialized
You can fix that by Adding code to initialize the variable.
Re: Bank Balance - while loop - wont compile
Look at my post if you still need help, it is linked below.
Initalizing Variables by TJStretch
Re: Bank Balance - while loop - wont compile
Sorry guys I'm still confused, I thought I have the variable initialised already ?
Re: Bank Balance - while loop - wont compile
Quote:
I thought I have the variable initialised already
The compiler doesn' think you have done it.
Can you post the code where you think you have initialized the variables in the error messages?
Do You understand there is a difference between defining a variable and giving it a value?
Re: Bank Balance - while loop - wont compile
Quote:
Originally Posted by
Tjstretch
Why the error occurs
In the code above, it is intuitively obvious that someVariable will be initialized. However, because Java will not hard-code that, the compiler does not have the same intuition. Suppose you modified the if-statement in the above example to use the condition "1 == 2", making the new program:
Code Java:
String someVariable;
if(1 == 2) //This could be a while-loop, for-loop, switch statement, etc.
{
someVariable = "Butterflies";
}
System.out.println(someVariable); //The error will be here
What is the compiler suppose to print out?
In that example, I define the variable but do not give it a value. You can go to that message linked above for how to fix it.
Here is, for all the compiler knows, what you are doing
Code Java:
double newBalance; //Declare the variable
while('a' == 'Y')
{
newBalance = 5; //This will never be called
break;
}
System.out.println(newBalance);
Re: Bank Balance - while loop - wont compile
char response = 'Y';
while (response == 'Y')
System.out.println("Do you want to make another balance ( Y or N)?");
response = keyboardIn.next().charAt(0);
This is where I thought I'd initialised the variable.
Re: Bank Balance - while loop - wont compile
Yes that variable is initialized. But what about the ones in the error messages in post #3?
Re: Bank Balance - while loop - wont compile
http://www.javaprogrammingforums.com...hile-loop.html
If you don't stop posting duplicate content, you will receive an infraction.
Re: Bank Balance - while loop - wont compile
System.out.println("Please enter the amount of your deposit");
deposit = keyboardIn.nextDouble();
System.out.println("Your new balance is" + newBalance);
newBalance = keyboardIn.nextDouble();
So these aren't initialised yet ?
Re: Bank Balance - while loop - wont compile
No. Those are assignment statements.
Where do you define the variables?
Read the post on the other thread where this was explained.
Re: Bank Balance - while loop - wont compile
Not if there is ANY possibility that the code will not be executed.
Are there any conditions that the controls if the code is executed?
Re: Bank Balance - while loop - wont compile
Ah now I get it, so I say newBalance =0 and deposit =0 ?
Re: Bank Balance - while loop - wont compile
You can give it a value when you define it. All in one statement.