Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Help

  1. Oct 4, 2006 #1
    When I compile this program I get the following error message:

    Discount.java:119: variable percent might not have been initialized
    System.out.print("You'll get a " + percent + " discount on ");
    ^

    WHAT THE HELL AM I DOING WRONG? :cry: :cry: :confused:
    Here is the source code:


    import java.util.*;

    public class Discount
    {
    public static void main(String[] args)
    {


    // Declare all named constants.

    final String LEVEL3 = "Gold"; // Name of top level
    final String PCT3 = "30"; // 30% discount for LEVEL3

    final String LEVEL2 = "Silver"; // Name of middle level
    final String PCT2 = "20"; // 20% discount for LEVEL2

    final String LEVEL1 = "Copper"; // Name of lowest level
    final String PCT1 = "10"; // 10% discount for LEVEL1



    // Declare a Scanner object for input.

    Scanner scan = new Scanner(System.in); // Standard input



    // Declare all other required objects.

    String
    name, // Name of customer
    level, // Membership level of customer
    percent; // Percent of discount


    // Declare all primitive variables.

    double
    totalPurchases; // Total amount of purchases





    // Prompt the user to type in the customer name,
    // then read and store the name.

    System.out.println("Please enter a customer name: ");
    name = scan.nextLine();




    // Prompt the user to type in the current member level,
    // then read and store the level.

    System.out.println("Please enter the customer's member level: ");
    level = scan.nextLine();



    // Prompt the user to type in the total amount of purchases,
    // then read and store that value.

    System.out.println("Please enter the ammount of purchase: ");
    totalPurchases = scan.nextDouble();



    // Print the header of the printout including the customer name.

    System.out.println("==========================");
    System.out.println(" Credit card member service");
    System.out.println(" ==========================");
    System.out.println("\nCustomer name: " + name);




    // Check if the given customer level matches with one of the
    // service levels the company provides.
    // If the given level does not match with one
    // of the given level names, print an error message.
    // Otherwise, according to the customer level,
    // print out the percentage discount they get.

    if (level == LEVEL3)
    percent = PCT3;
    else if (level == LEVEL2)
    percent = PCT2;
    else if (level == LEVEL1)
    percent = PCT1;
    else
    {
    System.out.println("We're sorry, but we can't recognize" +
    "your current member level. \nPlease contact " +
    "customer service immediately.");
    System.exit(0);
    }

    System.out.println("\nCongratulations!");
    System.out.print("You'll get a " + percent + " discount on ");
    System.out.print("your purchases\nduring the Thanksgiving holiday, ");
    System.out.println("which totaled " + totalPurchases);

    } // End public static void main

    } // End public class Discount
     
  2. jcsd
  3. Oct 4, 2006 #2
    Here is a sample output:

    Sample Input(1):
    Please enter a customer name: Steve
    Please enter the customer's member level: Silver
    Please enter the amount of purchases: 500.20

    Sample Output(1):
    ==========================
    Credit card member service
    ==========================

    Customer name: Steve
    Current member level: Silver

    Congratulations!
    You'll get a 20% discount on your purchases
    during the Thanksgiving holiday, which totaled $500.20.



    Sample Input(2):
    Please enter a customer Name : Gene
    Please enter the customer's member level: Diamond
    Please enter the amount of purchase: 200.45

    Sample Output(2):
    ==============================
    Credit card member service
    ==============================

    Customer name : Gene

    We're sorry, but we can't recognize your current member level.
    Please contact customer service immediately.
     
  4. Oct 4, 2006 #3

    0rthodontist

    User Avatar
    Science Advisor

    The variable may not have been initialized means that the compiler isn't sure it's been assigned a value yet. Apparently it can't decide whether in you necessarily must assign a value to percent while going through those if statements, or if it's possible to go through them without assigning anything to percent. In your definition, you could just write, for example,
    percent = "";
    which would assign a value to percent that you would never use. But it would make the compiler happy.
     
  5. Oct 4, 2006 #4
    Unfortunately I have tried that. I tried to intiatalize it when I declared it, but it just keeps that value no matter what happens.

    Like I say:

    char percent = "0";


    But then when it gets to that last output sentence it says:

    You'll get a 0% discount on your purchases
    during the Thanksgiving holiday, which totaled $500.20.
     
  6. Oct 4, 2006 #5

    0rthodontist

    User Avatar
    Science Advisor

    I think you mean String percent = "0";
    Another error that pops out at me is that you are asking if level == LEVEL2. In Java, nearly everything is a pointer to an object. So what you're asking is if the pointer to the string level points at the same place that the pointer to the string LEVEL2 points to. They don't, because they were created separately, even though they have the same contents.
    What you want to do to check two strings for character-by-character equality is use the .equals() method. If s1 and s2 are strings, you might ask if s1.equals(s2)
     
  7. Oct 4, 2006 #6
    Yes, That's what I meant.

    YES, YES, YES. This is the problem. Please help me out in finding out how I would write it that way. I would REALLY appreciate it.
     
  8. Oct 4, 2006 #7
    So would I write :

    if (level.equals(LEVEL3))
    percent = PCT3;
    else if (level.equals(LEVEL2))
    percent = PCT2;
    else if (level.equals(LEVEL1))
    percent = PCT1;
    else
    {
    percent = "0";
    System.out.println("We're sorry, but we can't recognize" +
    "your current member level. \nPlease contact " +
    "customer service immediately.");
    System.exit(0);
    }
     
  9. Oct 4, 2006 #8
    Thats It!!!!!!!!!!!!!!!!!!!!!!:tongue: :tongue: :tongue: :rofl: :rofl: :rofl: :biggrin: :biggrin: :biggrin: :biggrin: :redface: :smile: :smile: :bugeye: :rofl: :rolleyes: o:) o:) :wink: :smile: :smile: :smile: :smile: :redface: :rofl: :rolleyes: :tongue: :tongue2:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java Help
  1. Java help (Replies: 7)

  2. Java help (Replies: 15)

  3. Need help with Java (Replies: 53)

  4. Help! Cant get Java! (Replies: 3)

Loading...