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

  • Thread starter Thread starter Nothing000
  • Start date Start date
  • Tags Tags
    Percent Variable
AI Thread Summary
The discussion centers around a Java programming error related to the variable "percent" not being initialized before use. The error arises because the code checks membership levels using the "==" operator, which is inappropriate for string comparison in Java. Instead, the correct approach is to use the ".equals()" method for string equality checks. Participants suggest initializing "percent" to an empty string or a default value to avoid the compilation error. The proposed solution involves modifying the conditional statements to use "level.equals(LEVEL3)", "level.equals(LEVEL2)", and "level.equals(LEVEL1)" for proper string comparison. This adjustment resolves the initialization issue and ensures that the program functions correctly when determining the discount based on membership level.
Nothing000
Messages
403
Reaction score
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
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.
 
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.
 
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.
 
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)
 
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.
 
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);
}
 
Thats It!:-p :-p :-p :smile: :smile: :smile: :biggrin: :biggrin: :biggrin: :biggrin: :redface: :smile: :smile: :bugeye: :smile: :rolleyes: o:) o:) :wink: :smile: :smile: :smile: :smile: :redface: :smile: :rolleyes: :-p :-p
 

Similar threads

Replies
3
Views
3K
Replies
6
Views
8K
Replies
1
Views
7K
Replies
4
Views
3K
Back
Top