Results 1 to 5 of 5
- 06-01-2011, 09:54 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Error - message is already defined in main
Hi - I think I've got the code right, I just keep getting this one last error. Here is the code;
import java.util.Scanner;
public class ModifiedInvoiceApp
{
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
// 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";
int InvoiceCount = 0;
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(message);
// display the number of invoices, average invoice amount, and average discount amount
InvoiceCount = InvoiceCount + 1;
double averageDiscountAmount = discountAmount / InvoiceCount;
double averageInvoiceAmount = total / InvoiceCount;
String message = "\n" +
"Number Of Invoices: " + "\n"
+ "Average Invoice Amount: " + averageInvoiceAmount + "\n"
+ "Average Discount Amount: " + averageDiscountAmount + "\n";
System.out.println(message);
}
}
}
It is an invoice calculator, the application ends only when the user enters "n" or "N" (I think this part is right?), and it is supposed to display the number of invoices, average invoice amount, and average discount amount but of course I can't test all that out until I fix the error.
Here is the error: C:\Murach\Java6\Exercises\java1.6\ch02\ModifiedInv oiceApp.java:49: message is already defined in main(java.lang.String[])
String message = "\n" +
- 06-01-2011, 10:00 PM #2
You have got duplicate local variable "message" in the main method.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-01-2011, 10:07 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
What's the best way to change that? I'm very new to Java and programming in general, so sorry if that is a dumb question. Thank you!
- 06-01-2011, 10:11 PM #4
You are not allowed to have 2 local variables with the same name. So the solution is change either of them if they serve different purposes, use message1 and message2. Or if they both do the same thing then remove the type declaration from the second variable.
Hope that makes sense,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-01-2011, 10:24 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Ok, that helped! Everything works fine now, except I think my formulas for calculating the average invoice amount and the average discount amount are wrong, my numbers are not matching up. Can anyone help with the formulas?
import java.util.Scanner;
public class ModifiedInvoiceApp
{
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
// 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";
int InvoiceCount = 0;
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 message1 = "Discount percent: " + discountPercent + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + total + "\n";
System.out.println(message1);
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
// display the number of invoices, average invoice amount, and average discount amount
InvoiceCount = InvoiceCount + 1;
double averageInvoiceAmount = total / InvoiceCount;
double averageDiscountAmount = discountAmount / InvoiceCount;
String message2 = "\n" +
"Number Of Invoices: " + InvoiceCount + "\n"
+ "Average Invoice Amount: " + averageInvoiceAmount + "\n"
+ "Average Discount Amount: " + averageDiscountAmount + "\n";
System.out.println(message2);
}
}
}
Also, the message displaying the number of invoices, average invoice amount, and average discount is only supposed to show when the user ends the program. What am I doing wrong?
Similar Threads
-
Error: The Type Property Already Defined
By cest.lavie16 in forum New To JavaReplies: 2Last Post: 04-19-2011, 01:32 AM -
Error tag.getAsString:component context is not defined
By bbq in forum Advanced JavaReplies: 4Last Post: 01-05-2011, 03:00 PM -
Error message "could not find the main class"
By srwpchelp in forum New To JavaReplies: 8Last Post: 12-23-2010, 09:15 PM -
Error Message:Editor does not contain main type
By Deepa in forum New To JavaReplies: 9Last Post: 07-03-2009, 03:58 AM -
Error Message:Editor does not contain a main type
By Deepa in forum New To JavaReplies: 0Last Post: 11-27-2008, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks