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

    Default Variable to initialized

    This seems to function fine, but my running total is only outputting the most current price the user input. Any suggestions?

    Java Code:
    import java.util.Scanner;
    
    public class fff
    {
    	public static void main(String[] args)
    	{
    	
    	Scanner input = new Scanner (System.in);
    	double[]runningArray = new double[14];
    	
    	
    	
    	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("");
    	
    	for(int count=0; count < runningArray.length;  count++)
    	{
    	double runningtot = 0;
    	System.out.println("Please enter price of item: ");
    	double price = input.nextDouble();
                    System.out.println("");
                    System.out.println("That was $" + price);
                    System.out.println("");
                    runningtot = runningtot + price;
    				System.out.println("Your total price is $" + runningtot);
    	}
    	
    	
    	
    	}
    }

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

    Default Variable to initialized

    My running total variable is apparently not initialized and keeps printing out the most current number instead of a running total of all prices. Any suggestions?

    Around LINE 43

    Java Code:
    /*
    	Program:		Ahmed_Mohamed_Project2.java
    	Author:			A. Mohamed
    	Date:			Nov, 23/2012
    	Description:	A program that simulates a self checkout machine
    */
    
    import java.util.Scanner;
    
    public class fff
    {
    	public static void main(String[] args)
    	{
    	
    	Scanner input = new Scanner (System.in);
    	double[]runningArray = new double[14];
    	
    	
    	
    	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("");
    	
    	for(int count=0; count < runningArray.length;  count++)
    	{
    	double runningtot;
    	System.out.println("Please enter price of item: ");
    	double price = input.nextDouble();
                    System.out.println("");
                    System.out.println("That was $" + price);
                    System.out.println("");
                    runningtot = runningtot + price;
    				System.out.println("Your total price is $" + runningtot);
    	}
    	
    	
    	
    	}
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Need urgent help with Java Project

    Each time round that loop you are creating a new 'runningtot' variable.
    At the end of the loop that variable goes out of scope.

    If you want to keep it (which you do so you can add to it) then it needs to be declared outside of the loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Variable to initialized

    See your other thread.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Variable to initialized

    Quote Originally Posted by Tolls View Post
    See your other thread.
    I'm merging the two threads. Bungie, please go through the Forum Rules -- particularly the second paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    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 Tolls View Post
    Each time round that loop you are creating a new 'runningtot' variable.
    At the end of the loop that variable goes out of scope.

    If you want to keep it (which you do so you can add to it) then it needs to be declared outside of the loop.
    I took out double runningtot; from the for loop. But now the program is outputting the user's most current input instead of totaling the price's.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Variable to initialized

    Can you show your output?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Variable to initialized

    Quote Originally Posted by Tolls View Post
    Can you show your output?
    With a printscreen? Attached.Variable to initialized-variable.png

    Where ti says Total Price: it should add up all the previous prices.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Variable to initialized

    OK, so what does your code look like now?
    Specifically the bit around the loop (including the declaration of the runningtot variable).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Variable to initialized

    Quote Originally Posted by Tolls View Post
    OK, so what does your code look like now?
    Specifically the bit around the loop (including the declaration of the runningtot variable).
    Java Code:
    double runningtot;
    	Scanner input = new Scanner (System.in);
    	double[]runningArray = new double[14];
    .
    .
    .
    .
    .
    for(int count=0; count < runningArray.length;  count++)
    	{
    	
    	System.out.println("Please enter price of item: ");
    	double price = input.nextDouble();
                    System.out.println("");
                    System.out.println("That was $" + price);
                    System.out.println("");
                    runningtot =+ price;
    				System.out.println("Your total price is $" + runningtot);
    	}

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Variable to initialized

    That should be '+=' not '=+'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Variable may not have been initialized
    By gerchan in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 03:17 PM
  2. Variable not initialized?
    By seegee in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 03:00 PM
  3. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 10:28 AM
  4. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 04:27 PM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 10:02 AM

Posting Permissions

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