Results 1 to 20 of 20
- 11-26-2012, 10:53 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Need urgent help with Java Project
Hello, I have a project due soon for creating a self checkout machine in Java. The user inputs numbers that are added to a grand total and then taxed. My problem is at the end, when the user decides they bought enough things, they type -1 then the program goes straight to the calculation and if they type 0, the previous price is erased. I have the bare code of just adding the prices together but don't know how to erase the last number and stop the program to start the grand total calculation.
I'm supposed to use loops somewhere but only started learning about them. Any help would be appreciated. Thanks
---------------------(heres the barebones code but that shouldn't really matter, I just dont know what to type next)-----------------------
Java Code:import java.util.Scanner; public class aaa { public static void main(String[] args) { System.out.println("Big Box Depot Self-Serve Checkout"); System.out.println(""); System.out.println("Hi. Welcome to the checkout. What's your name? "); System.out.println(""); Scanner input = new Scanner (System.in); String name = input.nextLine(); System.out.println(""); System.out.println("Okay, " + name + ", enter the price of each purchase in dollars and cents, and then push the ENTER key. For example, if item costs $5.99 enter 5.99"); System.out.println(""); System.out.println("If you make a mistake when you enter a price enter a zero for the next entry and the last price you entered will be subtracted from your running total."); System.out.println(""); System.out.println("When you've entered all of your prices, enter -1 to indicate that you've finished your entries. I’ll then calculate what your total owing is."); System.out.println(""); System.out.println("Enter a price for item #1: "); System.out.println(""); Scanner input2 = new Scanner (System.in); Double price1 = input2.nextDouble(); System.out.println("Enter a price for item #1: " + price1); System.out.println(""); System.out.println("That was $" + price1 + ". Your total is $" + price1 + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #2: "); System.out.println(""); Scanner input3 = new Scanner (System.in); Double price2 = input3.nextDouble(); System.out.println("Enter a price for item #2: " + price2); if (price2 = 0) System.out.println(""); System.out.println("That was $" + price2 + ". Your total is $" + (price1 + price2) + "."); System.out.println("fin."); } }Last edited by Bungie; 11-26-2012 at 11:07 PM.
- 11-26-2012, 11:10 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Need urgent help with Java Project
Crossposted at Need urgent help with Java Project
- 11-26-2012, 11:25 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Oh, I thought that was another forum. Sorry about that I have lots of Java Forum tabs open so I thought it was on another site. Can you delete that? Not sure how forums work, yes I'm a noob.
- 11-26-2012, 11:51 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
- 11-27-2012, 12:37 AM #5
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: Need urgent help with Java Project
This thread is completely off-topic... either way, I'm going to go suggest you use "/n" to start a new line rather then " ". E.G.:
Java Code:System.out.println("Hello \n"); System.out.println("world.");Last edited by Darkzombies; 11-27-2012 at 12:44 AM.
-
Re: Need urgent help with Java Project
Why are you creating 3 different Scanner objects, all listening to the same object, System.in? Don't do that in this situation. Instead create one Scanner object and use it multiple times. When you've fixed this, we'll talk about how to handle the end of line token.
- 11-27-2012, 02:18 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Got that done. Lemme reprint code.
Java Code:import java.util.Scanner; public class aaa { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.println("Big Box Depot Self-Serve Checkout"); System.out.println(""); System.out.println("Hi. Welcome to the checkout. What's your name? "); System.out.println(""); String name = input.nextLine(); System.out.println(""); System.out.println("Okay, " + name + ", enter the price of each purchase in dollars and cents, and then push the \n"); System.out.println("ENTER key. For example, if item costs $5.99 enter 5.99"); System.out.println(""); System.out.println("If you make a mistake when you enter a price enter a zero for the next entry and the last \n"); System.out.println("price you entered will be subtracted from your running total."); System.out.println(""); System.out.println("When you've entered all of your prices, enter -1 to indicate that you've finished your entries. \n"); System.out.println("I’ll then calculate what your total owing is."); System.out.println(""); System.out.println("Enter a price for item #1: "); System.out.println(""); Double price1 = input.nextDouble(); System.out.println("Enter a price for item #1: " + price1); System.out.println(""); System.out.println("That was $" + price1 + ". Your total is $" + price1 + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #2: "); System.out.println(""); Double price2 = input.nextDouble(); System.out.println("Enter a price for item #2: " + price2); System.out.println(""); System.out.println("That was $" + price2 + ". Your total is $" + (price1 + price2) + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #3: "); System.out.println(""); Double price3 = input.nextDouble(); System.out.println("Enter a price for item #3: " + price3); System.out.println(""); System.out.println("That was $" + price3 + ". Your total is $" + (price1 + price2 + price3) + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #4: "); System.out.println(""); Double price4 = input.nextDouble(); System.out.println("Enter a price for item #4: " + price4); System.out.println(""); System.out.println("That was $" + price4 + ". Your total is $" + (price1 + price2 + price3 + price4) + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #5: "); System.out.println(""); Double price5 = input.nextDouble(); System.out.println("Enter a price for item #5: " + price5); System.out.println(""); System.out.println("That was $" + price5 + ". Your total is $" + (price1 + price2 + price3 + price4 + price5) + "."); System.out.println(""); } }
So the problem is; if the user enters 0, it must erase the last price they wrote, and if the user enters -1, it must compile all the prices into one grand total price. All the while it has something to do with using loops. I've played around with it but just can't figure it out. Any help would be greatly appreciated.Last edited by Bungie; 11-27-2012 at 02:33 AM.
-
Re: Need urgent help with Java Project
OK great. Now where are you adding any of the prices together? Also, don't use Double variables but rather double variables (the capitalization is important). Double is an object wrapper class for the primitive type, double.
- 11-27-2012, 02:38 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Where it says "your total price is (price1 + price 2)" etc. I don't know why I wrote Double as a capital. Lemme change that
-
Re: Need urgent help with Java Project
If it were my program, I'd create a double variable, grandTotal, and simply add the prices to it as I got them. That way when you convert this program to use a while loop, you can keep track of the total without having to create a thousand variables.
- 11-27-2012, 03:08 AM #11
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
- 11-27-2012, 03:19 AM #12
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Yeah I guess that could work. I could just change q to "-1" etc etc. Is there any way you could package that into the framework of my code? My instructor's always want it done specifically and every time I've tried there's lots of random errors. I usually program in C++ so I struggle with Java. Thanks. I'll keep trying and update you guys.
-
Re: Need urgent help with Java Project
Spoon fed answer deleted -- but too late. monkey jr, you're doing the OP no favors by doing this. Please help them learn to code, not to beg.
- 11-27-2012, 03:39 AM #14
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
I get the running total but what about the: if(x.equals("1")) part. I'm trying to make it so it keeps asking the question. So for example; WHat is the price fo item 1, then what is the price for item 2. But when they hit 0, it deletes that last price and continues. ANd -1 calculates everything. There's another part to the project but I don't understand how to do these 2. I guess I could use a do while loop but it would have to be something along the lines of, do calculations of items while the input of price does not euqal -1 becuase when it does, the program stops. The 0 part is not important since thats only 1 mark.
I have the general idea but my execution is pretty bad and I'm getting a few errors.
-
Re: Need urgent help with Java Project
If you're getting errors, please show us your latest code (not monkey's) and your error messages.
- 11-27-2012, 03:43 AM #16
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
- 11-27-2012, 03:47 AM #17
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
At this point the user can only input 3 prices instead of the 15 max that's needed. I know there's a way to make it so there's a 15 limit without writing a ton of useless code. Also, I'd like to use a do while loop until the user inputs -1 to stop the program and output a grand total. This is the barebones of it. (no errors)
Java Code:import java.util.Scanner; public class aaa { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.println("Big Box Depot Self-Serve Checkout"); System.out.println(""); System.out.println("Hi. Welcome to the checkout. What's your name? "); System.out.println(""); String name = input.nextLine(); System.out.println(""); System.out.println("Okay, " + name + ", enter the price of each purchase in dollars and cents, and then push the ENTER key. For example, if item costs $5.99 enter 5.99"); System.out.println(""); System.out.println("If you make a mistake when you enter a price enter a zero for the next entry and the last price you entered will be subtracted from your running total."); System.out.println(""); System.out.println("When you've entered all of your prices, enter -1 to indicate that you've finished your entries. I’ll then calculate what your total owing is."); System.out.println(""); System.out.println("Enter a price for item #1: "); System.out.println(""); double price1 = input.nextouble(); System.out.println("Enter a price for item #1: " + price1); System.out.println(""); System.out.println("That was $" + price1 + ". Your total is $" + price1 + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #2: "); System.out.println(""); double price2 = input.nextDouble(); System.out.println("Enter a price for item #2: " + price2); System.out.println(""); System.out.println("That was $" + price2 + ". Your total is $" + (price1 + price2) + "."); System.out.println(""); System.out.println(""); System.out.println("Enter a price for item #3: "); System.out.println(""); double price3 = input.nextDouble(); System.out.println("Enter a price for item #3: " + price3); System.out.println(""); System.out.println("That was $" + price3 + ". Your total is $" + (price1 + price2 + price3) + "."); System.out.println(""); } }
- 11-27-2012, 03:55 AM #18
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
Re: Need urgent help with Java Project
you could use a do while, and basically forget about number the price variables so no price1 + price 2 etc
and have one method that asks you the value of the item then keep a running total of those items
then add another item/price, again which will add the price variable to the running total and so forth
and the this will all be in the "do" and your "while" will say quit when user inputs -1.
the 0 remove previous price is also pretty simple, you could implement a way just before moving on to ask the user if they want to remove the price or continue, if they remove then it straight forward to figure out what to do with the total and such :)
look up nesting if's that might help you.Last edited by monkeyjr97; 11-27-2012 at 04:03 AM.
- 11-27-2012, 04:00 AM #19
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Yeah I think I got it. I'll play around with it. Thanks
- 11-27-2012, 05:45 AM #20
Member
- Join Date
- Nov 2012
- Posts
- 15
- Rep Power
- 0
Re: Need urgent help with Java Project
Encountered an error.
-- non-static method menu() cannot be referenced from a static context.
I tried to move the location of the line but to no avail.
LINE 31 and the public method is at the bottom.
Java Code:import java.util.Scanner; public class aaa { public static void main(String[] args) { Scanner input = new Scanner (System.in); double runningtot; double total; String x; x = input.nextLine(); System.out.println("Big Box Depot Self-Serve Checkout"); System.out.println(""); System.out.println("Hi. Welcome to the checkout. What's your name? "); System.out.println(""); String name = input.nextLine(); System.out.println(""); System.out.println("Okay, " + name + ", enter the price of each purchase in dollars and cents, and then push the ENTER key. For example, if item costs $5.99 enter 5.99"); System.out.println(""); System.out.println("If you make a mistake when you enter a price enter a zero for the next entry and the last price you entered will be subtracted from your running total."); System.out.println(""); System.out.println("When you've entered all of your prices, enter -1 to indicate that you've finished your entries. I’ll then calculate what your total owing is."); do { menu(); if(x.equals("1")) { System.out.println("Enter a price for item: \n"); double price = input.nextDouble(); System.out.println("Enter a price for item #1: " + price); System.out.println(""); System.out.println("That was $" + price + ". Your total is $" + price + "."); System.out.println(""); runningtot =+ price; } else if(x.equals("-1")) { total = runningtot; System.out.println("SUB-TOTAL: $" + total); } } while(!(x.equals("q"))||(x.equals("Q"))); } public void menu() { System.out.println("1 - add an item"); System.out.println("-1: Calculate Total"); System.out.println("q or Q - quit"); } }Last edited by Bungie; 11-27-2012 at 05:48 AM.
Similar Threads
-
Need urgent help with my first project Hello world app
By saif in forum New To JavaReplies: 16Last Post: 07-22-2012, 11:58 AM -
[URGENT] Due in 1.5 hrs. VERY easy java project question
By inimitablesikh in forum New To JavaReplies: 1Last Post: 09-29-2011, 06:38 AM -
Urgent! Need help for my final year project!!
By kazuma_riku73 in forum Sun Java Wireless ToolkitReplies: 7Last Post: 06-18-2010, 11:48 AM -
Project Question Urgent?
By newtocs in forum New To JavaReplies: 1Last Post: 10-09-2009, 02:55 PM -
Urgent! Need help for my final year project!!
By kazuma_riku73 in forum Sun Java Wireless ToolkitReplies: 1Last Post: 01-15-2009, 11:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks