#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0

    Can anyone help?


    So this is what my application should display (Note: You have to enter the two subtotals and press y to continue or n to not):

    Welcome to the Invoice Total Calculator

    Enter subtotal: 100
    Discount percent: 0.1
    Discount amount: 10.0
    Invoice total: 90.0

    Continue? (y/n): y

    Enter subtotal: 500
    Discount percent: 0.25
    Discount amount: 125.0
    Invoice total: 375.0

    Continue (y/n): n

    Number of invoices: 2
    Average invoice: 232.5
    Average discount: 67.5

    This is what it currently is displaying:


    Welcome to the Invoice Total Calculator

    Enter subtotal: 100
    Discount percent: 0.1
    Discount amount: 10.0
    Invoice total: 90.0

    Continue? (y/n): y

    Enter subtotal: 500
    Discount percent: 0.25
    Discount amount: 125.0
    Invoice total: 375.0

    Continue (y/n): n

    Number of invoices: 2
    Average invoice: 187.5
    Average discount: 62.5

    The Average invoice and Average discount are only accounting for the info when I enter "500" for the second subtotal. It doesn't even acknowledge the first subtotal where I enter "100". How do I fix this? My current code can be found below:



    import java.util.Scanner;
    public class InvoiceApp
    {
    public static void main(String[] args)
    {

    // welcome the user to the program
    System.out.println("Welcome to the Invoice Total Calculator");
    System.out.println(); // print a blank line

    // initialize variables
    int numberOfInvoices = 0;
    double averageInvoice = 0.0;
    double averageDiscount = 0.0;

    // create a Scanner object named sc
    Scanner sc = new Scanner(System.in);

    // perform invoice calculations until choice is equal to "n" or "N"
    String choice = "y";
    while (!choice.equalsIgnoreCase("n"))

    {
    // get the invoice subtotal from the user
    System.out.print("Enter subtotal: ");
    double subtotal = sc.nextDouble();

    // calculate the discount amount and total
    double discountPercent= 0.0;
    if (subtotal >= 500)
    discountPercent = .25;
    else if (subtotal >= 100)
    discountPercent = .1;
    else
    discountPercent = 0.0;
    double discountAmount = subtotal * discountPercent;
    double total = subtotal - discountAmount;

    // display the discount amount and total
    String message = "Discount percent: " + discountPercent + "\n"
    + "Discount amount: " + discountAmount + "\n"
    + "Invoice total: " + total + "\n";
    System.out.println(message);

    // see if the user wants to continue
    System.out.print("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();

    {
    numberOfInvoices = numberOfInvoices + 1;
    averageInvoice = total / numberOfInvoices;
    averageDiscount = discountAmount / numberOfInvoices;
    }

    }
    //displays the invoice numbers, average invoice amount, and average discount
    String message2 = "Number of invoices: " + numberOfInvoices + "\n"
    + "Average invoice: " + averageInvoice + "\n"
    + "Average discount: " + averageDiscount + "\n";
    System.out.println(message2);

    }
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    I don't understand where the problem is. Could you give an idea of what you expect?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by bullet
    I don't understand where the problem is. Could you give an idea of what you expect?
    I don't understand where the problem is either, but the averageDiscount and averageInvoice are supposed to add the information from the first set of values I enter "100" and the second set of values I enter "500". The averageDiscount would add 10.0 and 125.0 while the averageInvoice would add 90.0 and 375.0 (this is what I want it to do) and then it would divide the numberOfInvoices (2) to find the average which would be 67.5 for averageDiscount and 232.5 for averageInvoice.

    Instead of doing that, it is only taking the information from the last set of values I enter "500" and the discount amount under that is 125.0 and it is dividing the numberOfInvoices (which is 2) to come up with 62.5 instead of 67.5. The same thing is happening with the averageInvoice where it divides 375.0 (Invoice total) by the numberOfInvoices (2) to get 187.5 when I'm trying to get it to equal 232.5.

    So basically my problem is that my averageInvoice and averageDiscount codes aren't acknowledging the first set of values where I enter "100" and it is only acknowledging the last set of values where I enter "500". I hoped that help. Best way I could explain it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    I think this is what you want:

    Code:
    import java.util.Scanner;
    public class invoiceApp
    {
    	public static void main(String[] args)
    	{
    
    		// welcome the user to the program
    		System.out.println("Welcome to the Invoice Total Calculator");
    		System.out.println(); // print a blank line
    
    		// initialize variables
    		int numberOfInvoices = 0;
    		double totalDiscount = 0;
    		double totalInvoice=0;
    		// create a Scanner object named sc
    		Scanner sc = new Scanner(System.in);
    
    		// perform invoice calculations until choice is equal to "n" or "N"
    		String choice = "y";
    		while (!choice.equalsIgnoreCase("n"))
    
    		{
    			// get the invoice subtotal from the user
    			System.out.print("Enter subtotal: ");
    			double subtotal = sc.nextDouble();
    
    			// calculate the discount amount and total
    			double discountPercent= 0.0;
    			if (subtotal >= 500)
    				discountPercent = .25;
    			else if (subtotal >= 100)
    				discountPercent = .1;
    			
    			double discountAmount = subtotal * discountPercent;
    			double total = subtotal - discountAmount;
    			totalDiscount += discountAmount;
    			totalInvoice += total;
    
    			// display the discount amount and total
    			String message = "Discount percent: " + discountPercent + "\n"
    			+ "Discount amount: " + discountAmount + "\n"
    			+ "Invoice total: " + total + "\n";
    			System.out.println(message);
    
    			// see if the user wants to continue
    			System.out.print("Continue? (y/n): ");
    			choice = sc.next();
    			System.out.println();
    
    
    			numberOfInvoices ++;
    		}
    
    		double averageInvoice = totalInvoice / numberOfInvoices;
    		double averageDiscount = totalDiscount / numberOfInvoices;
    
    		//displays the invoice numbers, average invoice amount, and average discount
    		String message2 = "Number of invoices: " + numberOfInvoices + "\n"
    		+ "Average invoice: " + averageInvoice + "\n"
    		+ "Average discount: " + averageDiscount + "\n";
    		System.out.println(message2);
    
    	}
    }
    You were not using a total amount and recalculating only the average of the last entered invoice.

    Note: a do-while loop would be more appropiate here, since it runs 1 or more times. A while loop is designed to run 0 or more times.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by Aurum84
    I think this is what you want:

    Code:
    import java.util.Scanner;
    public class invoiceApp
    {
    	public static void main(String[] args)
    	{
    
    		// welcome the user to the program
    		System.out.println("Welcome to the Invoice Total Calculator");
    		System.out.println(); // print a blank line
    
    		// initialize variables
    		int numberOfInvoices = 0;
    		double totalDiscount = 0;
    		double totalInvoice=0;
    		// create a Scanner object named sc
    		Scanner sc = new Scanner(System.in);
    
    		// perform invoice calculations until choice is equal to "n" or "N"
    		String choice = "y";
    		while (!choice.equalsIgnoreCase("n"))
    
    		{
    			// get the invoice subtotal from the user
    			System.out.print("Enter subtotal: ");
    			double subtotal = sc.nextDouble();
    
    			// calculate the discount amount and total
    			double discountPercent= 0.0;
    			if (subtotal >= 500)
    				discountPercent = .25;
    			else if (subtotal >= 100)
    				discountPercent = .1;
    			
    			double discountAmount = subtotal * discountPercent;
    			double total = subtotal - discountAmount;
    			totalDiscount += discountAmount;
    			totalInvoice += total;
    
    			// display the discount amount and total
    			String message = "Discount percent: " + discountPercent + "\n"
    			+ "Discount amount: " + discountAmount + "\n"
    			+ "Invoice total: " + total + "\n";
    			System.out.println(message);
    
    			// see if the user wants to continue
    			System.out.print("Continue? (y/n): ");
    			choice = sc.next();
    			System.out.println();
    
    
    			numberOfInvoices ++;
    		}
    
    		double averageInvoice = totalInvoice / numberOfInvoices;
    		double averageDiscount = totalDiscount / numberOfInvoices;
    
    		//displays the invoice numbers, average invoice amount, and average discount
    		String message2 = "Number of invoices: " + numberOfInvoices + "\n"
    		+ "Average invoice: " + averageInvoice + "\n"
    		+ "Average discount: " + averageDiscount + "\n";
    		System.out.println(message2);
    
    	}
    }
    You were not using a total amount and recalculating only the average of the last entered invoice.

    Note: a do-while loop would be more appropiate here, since it runs 1 or more times. A while loop is designed to run 0 or more times.

    Dude, thanks so much. You need to teach me your ways haha. I'm really new with this, so I have a couple questions.

    Why did you decide to put

    numberOfInvoices ++;

    instead of

    numberOfInvoices = numberOfInvoices + 1;

    ???

    Also, did you give me a while or do-while loop?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    If you can answer this question too, that would be great!

    Modify the code so the application ends only when the user enters "n" or "N" . As it is now, the application ends when the users enters anything other than "y" or "Y". To do this, you need to use a not operator (!) with the equalsIgnoreCase method. Then, compile this class and test this change by entering 0 at the Continue prompt.
    This is the original code:

    // perform invoice calculations until choice isn't equal to "y" or "Y"
    String choice = "y";
    while (choice.equalsIgnoreCase ("y"))
    This is what I updated it to:

    // perform invoice calculations until choice is equal to "n" or "N"
    String choice = "y";
    while (!choice.equalsIgnoreCase("n"))
    So my main question is did I do this correctly? It ends when I put in "n" or "N", but it continues even if I don't put "y" or "Y". It'll continue even when I put in "0" or really anything for that matter.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    How about:
    Code:
    while(!choice.equalsIgnoreCase("n")){
    if(choice.equalsIgnoreCase("y"))
    {
    
    }
    }
    Last edited by Aurum84; February 8th, 2013 at 01:36 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by Aurum84
    How about:
    Code:
    while(!choice.equalsIgnoreCase("n")){
    if(choice.equalsIgnoreCase("y"))
    {
    
    }
    }
    Thank you so much! Seriously dude, you helped me a lot. You don't even know. I've literally been trying to figure this all out for hours haha.

IMN logo majestic logo threadwatch logo seochat tools logo