Results 1 to 5 of 5
  1. #1
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default 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" +

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    You have got duplicate local variable "message" in the main method.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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!

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by dimesnnix View Post
    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!
    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,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    dimesnnix is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  1. Error: The Type Property Already Defined
    By cest.lavie16 in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 01:32 AM
  2. Replies: 4
    Last Post: 01-05-2011, 03:00 PM
  3. Error message "could not find the main class"
    By srwpchelp in forum New To Java
    Replies: 8
    Last Post: 12-23-2010, 09:15 PM
  4. Error Message:Editor does not contain main type
    By Deepa in forum New To Java
    Replies: 9
    Last Post: 07-03-2009, 03:58 AM
  5. Replies: 0
    Last Post: 11-27-2008, 12:25 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
  •