February 8th, 2013, 11:24 AM
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):
This is what it currently is displaying:
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:
February 8th, 2013, 11:50 AM
I don't understand where the problem is. Could you give an idea of what you expect?
February 8th, 2013, 12:18 PM
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.
Originally Posted by bullet
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.
February 8th, 2013, 12:50 PM
I think this is what you want:
You were not using a total amount and recalculating only the average of the last entered invoice.
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;
// 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";
// 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";
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
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";
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.
February 8th, 2013, 01:13 PM
Originally Posted by Aurum84
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 = numberOfInvoices + 1;
Also, did you give me a while or do-while loop?
February 8th, 2013, 01:22 PM
If you can answer this question too, that would be great!
This is the original code:
This is what I updated it to:
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.
February 8th, 2013, 02:32 PM
Last edited by Aurum84; February 8th, 2013 at 02:36 PM.
February 8th, 2013, 03:26 PM
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.
Originally Posted by Aurum84