Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Bank Balance - while loop - wont compile

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Bank Balance - while loop - wont compile

    // 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
    Last edited by mwardjava92; November 9th, 2011 at 08:48 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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;
    ^

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bank Balance - while loop - wont compile

    variable newBalance might not have been initialized
    You can fix that by Adding code to initialize the variable.

  5. The Following User Says Thank You to Norm For This Useful Post:

    mwardjava92 (November 9th, 2011)

  6. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Bank Balance - while loop - wont compile

    Look at my post if you still need help, it is linked below.

    Initalizing Variables by TJStretch
    Last edited by Tjstretch; November 9th, 2011 at 12:33 PM.

  7. #6
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Bank Balance - while loop - wont compile

    Sorry guys I'm still confused, I thought I have the variable initialised already ?

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bank Balance - while loop - wont compile

    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?

  9. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Bank Balance - while loop - wont compile

    Quote Originally Posted by Tjstretch View Post
    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:
    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
    double newBalance; //Declare the variable
    while('a' == 'Y')
    {
      newBalance = 5; //This will never be called
      break;
    }
    System.out.println(newBalance);
    Last edited by Tjstretch; November 9th, 2011 at 12:44 PM.

  10. #9
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bank Balance - while loop - wont compile

    Yes that variable is initialized. But what about the ones in the error messages in post #3?

  12. #11
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #12
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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 ?

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  16. #15
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Bank Balance - while loop - wont compile

    Ah now I get it, so I say newBalance =0 and deposit =0 ?

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bank Balance - while loop - wont compile

    You can give it a value when you define it. All in one statement.

  18. The Following User Says Thank You to Norm For This Useful Post:

    mwardjava92 (November 9th, 2011)

Similar Threads

  1. Bank Balance - while loop
    By mwardjava92 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 08:16 AM
  2. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  3. anagram program wont compile please help
    By Rusak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2011, 06:23 PM
  4. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 02:38 PM
  5. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM