Discount.java:119: variable percent might not have been initialized

In summary, the programmer is getting an error message when compiling the program. When they try to run the program, it prints out a header and then says that the customer's member level does not match one of the service levels provided by the company. The program then gives them a percentage discount based on their purchases during the Thanksgiving holiday.
  • #1
Nothing000
403
0
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 amount 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
 
Technology news on Phys.org
  • #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.
 
  • #3
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.
 
  • #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.
 
  • #5
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)
 
  • #6
0rthodontist said:
I think you mean String percent = "0";

Yes, That's what I meant.

0rthodontist said:
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)

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.
 
  • #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);
}
 
  • #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:
 

1. What does "variable percent might not have been initialized" mean?

This means that the variable named "percent" has not been given a value before it is being used in the code.

2. How can I fix the error "variable percent might not have been initialized"?

To fix this error, you need to assign a value to the "percent" variable before using it in the code. This can be done by initializing the variable with a value or by assigning a value to it before using it.

3. Why is the error "variable percent might not have been initialized" occurring?

This error is occurring because the "percent" variable is being used before it has been given a value. In Java, variables must be initialized before they can be used.

4. Is it necessary to initialize all variables before using them in Java?

Yes, it is necessary to initialize all variables before using them in Java. This helps to avoid errors like "variable percent might not have been initialized" and ensures that the code runs smoothly.

5. Can I use a variable without initializing it in Java?

No, you cannot use a variable without initializing it in Java. This is because Java requires all variables to be given a value before they can be used in the code.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top