Reputation: 11
Total beginner to Java! I need to find out why 'discount' and 'discountnum' keep saying they need to be initialized when I'm initializing in the if else statements. I don't want to take user input for those two like I do for my other variables. How can I fix this so it just posts the numbers?
import java.util.Scanner;
package com.company;
public class Coffee {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
double prevpurchases = scan.nextDouble();
double saleamount = scan.nextDouble();
double discountnum;
double totalamount;
int discount;
double amountsaved;
System.out.println("Welcome to Starbucks.\n"+
"Please enter your name: " + name);
System.out.println("Please enter the current purchase amount: " + saleamount);
System.out.println("Please enter the number of purchases in the last month: " + prevpurchases);
if (saleamount < 5){
discountnum = 0;
amountsaved = 0.00;
} else {
if (prevpurchases == 0) {
discountnum = 0.05;
discount = 5;
}
else if (prevpurchases >= 1 && prevpurchases <= 5){
discountnum = 0.10;
discount = 10;
}
else if (prevpurchases >= 6 && prevpurchases <= 10){
discountnum = 0.15;
discount = 15;
}
else if (prevpurchases >= 11 && prevpurchases <= 15){
discountnum = 0.20;
discount = 20;
}
else if (prevpurchases >= 16 && prevpurchases <= 20){
discountnum = 0.25;
discount = 25;
}
else if (prevpurchases >= 21){
discountnum = 0.30;
discount = 30;
}
}
System.out.println("***********************\n" +
"Receipt for " + name);
System.out.println("You get a " + discount + "% discount.");
amountsaved = saleamount * discountnum;
totalamount = saleamount - amountsaved;
System.out.println("Total amount due: $ " + totalamount);
System.out.println("You saved $ " + amountsaved);
System.out.println("***********************\n");
}
}
EXAMPLE OUTPUT:
Welcome to Starbucks.
Please enter your name: Brian B Bryan
Please enter the current purchase amount: $ 12.78
Please enter the number of purchases in the last month: 14
Receipt for Brian B Bryan
You get a 20% discount.
Total amount due: $ 10.22
You saved $ 2.56
Upvotes: 1
Views: 235
Reputation: 345
The issue at hand can indeed be fixed by either of the other peoples' suggestions. In addition to giving you a quick fix, I would like to give some intuition on why you get the errors:
The compiler,i.e. the program which (simplified) goes from your Java code--> Java byte code (stripped down version of what you wrote) --> Machine code (0's and 1's) has to enforce certain rules. As Java is a high level, object-oriented programming language, it hides a lot of details from you. For example, you never have to worry about where in memory something is stored, you will be alerted once you go out of bounds of an array, and after using an object you never have to get rid of it manually (i.e. free up the space occupied by it).
Sounds great, right? Well, the issue is that Java, by doing that has to check things for you that in other programming languages (e.g. C) the programmer has to manage. Although you might know that a certain path through your if statements will never be taken, the compiler cannot be 100% sure in the code you provided. Let's look at the following example:
int lel;
boolean test = true;
if(test)
{
lel = 10;
}
System.out.println(lel);
Even this simplistic snipped will throw the compiler off.
The underlying problem of what I have described above is actually more technical, namely it is the case that variables are not evaluated at compiletime, but at runtime. In a nutshell, the compiler treats lel
and test
as a blackbox holding a value of the type. Which values they hold is of no concern at compilation.
Hence, the compiler does not know that test
really is always true. On the other hand, if you were to write:
int lel;
if(true)
{
lel = 10;
}
the compiler, as true is not a variable but a value, evaluates it at compilation. Therefore, you do not get an error. The last example is the use if the final
keyword. final
means, that a value will not be changed after its definition. As this basically equates to the variable being constant, the compiler actually "looks" at the value of the variable and does not treat it as a blackbox. I.e., if you were to replace boolean test = true;
with final boolean test = true;
, the compiler will treat test
as a value. Hence, it is already clear at compile time, that the if statement is always evaluated to true.
In either of the two examples (i.e. if (true){...}
and final boolean test = true;
, the compiler will most certainly realize that the if statement around lel = 10;
is useless and rewrite it to:
int lel;
lel = 10;
Upvotes: 0
Reputation: 155
You "might" have a case, which will not pass any of your if-else-if. Try with this code.
if (saleamount < 5){
discountnum = 0;
amountsaved = 0.00;
} else {
if (prevpurchases == 0) {
discountnum = 0.05;
discount = 5;
}
else if (prevpurchases >= 1 && prevpurchases <= 5){
discountnum = 0.10;
discount = 10;
}
else if (prevpurchases >= 6 && prevpurchases <= 10){
discountnum = 0.15;
discount = 15;
}
else if (prevpurchases >= 11 && prevpurchases <= 15){
discountnum = 0.20;
discount = 20;
}
else if (prevpurchases >= 16 && prevpurchases <= 20){
discountnum = 0.25;
discount = 25;
}
else if (prevpurchases >= 21){
discountnum = 0.30;
discount = 30;
} else {
//this is the missing/needed initialization
//if neither one of the tests you have above, you need to somehow initialize the variable.
}
}
Upvotes: 1
Reputation: 313
Initialize them in the variable declaration with a value, like this:
double discountnum = 0;
double totalamount = 0;
int discount = 0;
double amountsaved = 0;
Upvotes: 0