HomeОбразованиеRelated VideosMore From: Kakra Detome

4.16. Budget Analysis - Java

16 ratings | 2288 views
Starting out with Java: From control structures through objects Chapter 4 Programming Challenges 16. Budget Analysis Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her expenses for the month, and keep a running total. When the loop finishes, the program should display the amount that the user is over or under budget. Gaddis, Tony (2015-05-29). Starting Out with Java: From Control Structures through Objects (Page 265). Pearson Education. Kindle Edition. Thanks :)
Html code for embedding videos on your blog
Text Comments (9)
Jeann Legaspi (20 days ago)
when I try over budget under budget it still appears idunno why
Super9jagirl (7 months ago)
You are the best. Thank you.
Geoffrey Zoref (11 months ago)
LOL, I spent like 3 hour son this assuming that it wanted us to enter the name of the expsnse, like rent and food, along with the cost. So I finally finished it and I'm happy with it, but it would have been simpler if I had known that it only wanted the expenses! I has for loops and everything!!
iReiGN xx (2 years ago)
Your explanation of the keyboard buffer was a bit confusing at first, but as you continued your explanation and used the visual aid, it became easy to understand. Well done!
Kakra Detome (2 years ago)
Hello, sorry if it was confusing but I'm glad you got it :) I'm teaching Java now, Learn Java, https://www.youtube.com/playlist?list=PLGFLoZ9w5xCMDOxkwYMQ4jbqisJxdc4Q6 and will try everything to be clearer when I talk about the keyboard buffer. Thanks for your nice words :) Take care and please let me know if you have any questions ok :)
e. d (2 years ago)
Hey ! I would like to know if it is possible to do the same program but instead of entering (N) NO to end the program, what about using a Sentinel Value to end it, like enter -100 or anykind of negative value ! If so what do u need to change in the code! Thank you (Y)
Kakra Detome (2 years ago)
Hi, the order your code is, in the while loop will give you errors. The way you've set it up. If the user enters let's say, 2 for userExpense, the user expense will be added to the totalExpenses alright and the message asking the user to enter an expense will be displayed again while the userExpense is not equal to -100. Now if the user enters -100, -100 will be added to totalExpenses(will in actual sense be subtracted) before your code checks to see if -100 is a valid userExpense. What I'm saying is, the userExpense, even if it's -100, will be added to the totalExpenses(will in actual sense be subtracted) before your code checks to see if the userExpense is not equal to -100. You want to ask and check before you add the userExpense to the totalExpenses. Also, don't set the totalExpenses to 1 because that will mean that the actual totalExpenses when the user finishes entering all his/her expenses will the totalExpenses of the user, plus the 1 you initialized totalExpenses to. Before the user starts entering expenses, you want the user's total expenses to be 0. Please change these or you'll get errors. Hope it helps. Please let me know if you have any issues ok. Take care :)
e. d (2 years ago)
Thank u so much! i did it, but instead of : while( userExpense != -100 ) { totalExpenses = totalExpenses + userExpense; System.out.println( "Enter an expense or -100 if you don't have any more expenses: " ); userExpense = scanner.nextDouble(); } i did this : while( userExpense != -100 ) { System.out.println( "Enter an expense or -100 if you don't have any more expenses: " ); userExpense = scanner.nextDouble(); totalExpenses = totalExpenses + userExpense; } witch was okay, but instead of totalExpenses=0 i did it with totalExpenses =1 and it gave me the same result in the end. Thanks again for your time and your solution :). I will ask again, if i have any trouble in the future (Y).
Kakra Detome (2 years ago)
Hi, sorry for the late response. If you want the program to keep asking the user to enter an expense and only stop when the user hits for example -100, we won't need the moreExpenses variable. We can store the user's response in the userExpense variable, like we're doing already and allow the loop to run as long as what's stored in the userExpense variable is not -100. Meaning, as soon as the user enters -100, the while loop that keeps on asking the user to enter an expense will be exited and the output of whether or not the user was over budget or used exactly the budget, will be displayed. The while loop will be modified to this based on the explanation above, while( userExpense != -100 ) { totalExpenses = totalExpenses + userExpense; System.out.println( "Enter an expense or -100 if you don't have any more expenses: " ); userExpense = scanner.nextDouble(); } The very first time the program starts, userExpense won't have a value when it's being tested to see if it's not equal to 100 so please initialize it to 0 so it has a value. The exclamation sign means, not, so != means not equal to. The while loop boolean expression is checking to see if userExpense is not equal to -100. Hope this helps. Please let me know if it doesn't ok. Take care :)

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.