Results 1 to 20 of 20
  1. #1
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default 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-27-2012 at 12:07 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Need urgent help with Java Project


  3. #3
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

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

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Need urgent help with Java Project

    Quote Originally Posted by Bungie View Post
    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.
    They aren't the same site. Some members contribute to both, others don't. Posting in both without being explicit about doing so can be considered rude in that if you receive an answer in one, many in the other may not know about it.

  5. #5
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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 01:44 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

  7. #7
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: Need urgent help with Java Project

    Quote Originally Posted by Fubarable View Post
    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.
    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 03:33 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

  9. #9
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default 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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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. #11
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Need urgent help with Java Project

    Quote Originally Posted by Fubarable View Post
    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.
    Does using the primitive type and the wrapper class give a different result regarding the "+" operator? I think it would be converted back between the primitive and object by the auto boxing mechanism.

  12. #12
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: Need urgent help with Java Project

    Quote Originally Posted by monkeyjr97 View Post
    couldn't you wrap the whole thing in a while loop, and use a nice looking user menu and exit it when the user inputs say "q" || "Q" - (QUIT- just an eg) it will run the calculation and display an output.

    Im a noob myself, and its 2am and im a bit tired so excuse any minor errors!
    you could do something like:
    Java Code:
    iimport java.util.*;
    public class aaa{
        private static double price;
        private static double total;
        private Scanner input;
        public static void main(String[] args)
        {
            Scanner input=new Scanner(System.in);
            String name = input.nextLine();
            double runningtot;
            String x;
    
            do{
    
                System.out.println("1 - add an item");
                System.out.println("2- calc total");
                System.out.println("q or Q - quit");
                x=input.nextLine();
                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;
                    total=runningtot;
                }
                else if(x.equals("2")){
                   
                    System.out.println("total value of basket:$"+total);
                }
            }while(!(x.equals("q"))||(x.equals("Q")));
    
        }
    
    
        }
    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.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

  14. #14
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

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

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

  16. #16
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: Need urgent help with Java Project

    Quote Originally Posted by Fubarable View Post
    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.
    This isn't the answer I wanted. I don't understand half of it and it compiles with loads of errors so it's pretty useless. Sorry for any inconvenience.

  17. #17
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default 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("");
    	
    	}
    }

  18. #18
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default 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 05:03 AM.

  19. #19
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: Need urgent help with Java Project

    Yeah I think I got it. I'll play around with it. Thanks

  20. #20
    Bungie is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default 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 06:48 AM.

Similar Threads

  1. Replies: 16
    Last Post: 07-22-2012, 12:58 PM
  2. [URGENT] Due in 1.5 hrs. VERY easy java project question
    By inimitablesikh in forum New To Java
    Replies: 1
    Last Post: 09-29-2011, 07:38 AM
  3. Urgent! Need help for my final year project!!
    By kazuma_riku73 in forum Sun Java Wireless Toolkit
    Replies: 7
    Last Post: 06-18-2010, 12:48 PM
  4. Project Question Urgent?
    By newtocs in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:55 PM
  5. Urgent! Need help for my final year project!!
    By kazuma_riku73 in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 01-15-2009, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •