Reputation: 41
package assignment_3_1;
import java.util.Scanner;
public class Assignment_3_1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner (System.in);
//obtain package weight 1
System.out.print("Enter the Package Weight (In Pounds): ");
int packageWeight1 = input.nextInt();
double WeightCalc1 = 5;
double WeightCalc2 = 15;
double WeightCalc3 = 34;
double WeightCalc4 = 45;
double WeightCalc5 = 60;
double WeightCalc6 = 60;
double priceA = 12;
double priceB = 14;
double priceC = 17;
double priceD = 21;
double priceE = 33;
double priceF = 105;
//if WeightCalc1 >= packageWeight1 the base charge is 12
if (WeightCalc1 >= packageWeight1)
{
System.out.println("The Base Charge is : " + priceA);
int basePrice = 12;
}
else
{
//if WeightCalc2 >= packageWeight1 the base charge is 14
if (WeightCalc2 >= packageWeight1)
{
System.out.println("The Base Charge is: " + priceB);
int basePrice = 14;
}
else
{
//if WeightCalc3 >= packageWeight1 the base charge is 17
if (WeightCalc3 >= packageWeight1)
{
System.out.println("The Base Charge is: " + priceC);
int basePrice = 17;
}
else
{
//if weightCalc4 >= packageWeight1 the base charge is 21
if (WeightCalc4 >= packageWeight1)
{
System.out.println("The base charge is: " + priceD);
int basePrice = 21;
}
else
{
//if weightCalc5 >= packageWeight1 the base charge is 33
if (WeightCalc5 >= packageWeight1)
{
System.out.println("The base charge is: " + priceE );
int basePrice = 33;
}
else
{
//if weightCalc6 < packageWeight1 the base charge is 105
if (WeightCalc6 < packageWeight1)
{
System.out.println("The base charge is: " + priceF);
int basePrice = 105;
}
else
{
System.out.println("Re-Run the Program");
}
}
}
}
}
}
//obtain zipCode
System.out.println("Enter your 5 Digit Zip Code: ");
int zipCode = input.nextInt();
double perc1 = 3999;
double perc2 = 5000;
double perc3 = 5999;
double perc4 = 7000;
//if perc1 < basePrice < perc2
if (perc1 < basePrice < perc2)
{
}
}
}
i declared an int inside of the if statement and when i started writing the bottom part after the large if statement i tried using basePrice the int i declared inside of the if statement i've tried changing the name of the int and using a double instead of an int and nothing will work not sure what im doing wrong.
Upvotes: 1
Views: 99
Reputation: 7069
Variable's scope is limited to that if only.
You can not access it outside that if condition.
You can declare it global to access it in another if.
Update
int basePrice = 0; // declare
and after that assign values in other if conditions.
if (WeightCalc1 >= packageWeight1)
{
System.out.println("The Base Charge is : " + priceA);
// int basePrice = 12; //--not like this
basePrice = 12; // assign
}
Upvotes: 0
Reputation: 16833
basePrice has to be declared at the beginning of the main
Here is some refactoring :
private static Integer checkWeight(int packageWeight, int weightCalc, int price)
{
if (weightCalc >= packageWeight)
{
System.out.println("The Base Charge is: " + price);
return price;
}
else
{
return null;
}
}
public static void main(String[] args)
{
//create a scanner
Scanner input = new Scanner (System.in);
//obtain package weight 1
System.out.print("Enter the Package Weight (In Pounds): ");
int packageWeight1 = input.nextInt();
int[] weightCalcs = {5, 15, 34, 45, 60, 60};
int[] prices = {12, 14, 17, 21, 33, 105};
Integer basePrice = null;
for (int i = 0; i < weightCalcs.length; i++)
{
if ((basePrice = checkWeight(packageWeight1, weightCalcs[i], prices[i])) != null)
{
// price found !
break;
}
}
if (basePrice != null)
{
//obtain zipCode
System.out.println("Enter your 5 Digit Zip Code: ");
int zipCode = input.nextInt();
double perc1 = 3999;
double perc2 = 5000;
double perc3 = 5999;
double perc4 = 7000;
// TODO using basePrice for the next intructions
}
else
{
System.out.println("Re-Run the Program");
}
}
Upvotes: 0
Reputation: 1263
Code having a problem with basePrice
. You are declaring basePrice
inside if
& else
and you are using it outside it. Try declaring as global variable inside main
method so that you can access from anywhere in main
.
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner (System.in);
int basePrice = 0;
// Your rest of code will be here ....
}
Upvotes: 0
Reputation: 1723
The problem is that every int basePrice
is declared in it's own scope and does not exist beyond that scope. You want to declare the int
with all your doubles, like this:
double WeightCalc1 = 5;
double WeightCalc2 = 15;
double WeightCalc3 = 34;
double WeightCalc4 = 45;
double WeightCalc5 = 60;
double WeightCalc6 = 60;
double priceA = 12;
double priceB = 14;
double priceC = 17;
double priceD = 21;
double priceE = 33;
double priceF = 105;
// declare basePrice here
int basePrice;
Then instead of having these blocks:
//if WeightCalc1 >= packageWeight1 the base charge is 12
// base price only exists within these brackes
if (WeightCalc1 >= packageWeight1)
{
// base price only exists within these brackes
System.out.println("The Base Charge is : " + priceA);
int basePrice = 12;
}
You change to this:
//if WeightCalc1 >= packageWeight1 the base charge is 12
if (WeightCalc1 >= packageWeight1)
{
System.out.println("The Base Charge is : " + priceA);
basePrice = 12;
}
Upvotes: 1
Reputation: 6816
In java the scope of a variable is local to a block. A block is generally code between {...}. If you need to use it out side the bold then you should declare it outside the block. See examples for details.
if(condition){
int i=10;
}
You cannot use i outside the if.
If you wish to the you should do something like
int i =0;
if(condition)
{
i=10;
}
Now you can use it outside the if.
Upvotes: 1
Reputation: 4609
declare it outside the if because u are restricting its scope to that if only
package assignment_3_1;
import java.util.Scanner;
public class Assignment_3_1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner (System.in);
//obtain package weight 1
System.out.print("Enter the Package Weight (In Pounds): ");
int packageWeight1 = input.nextInt();
double WeightCalc1 = 5;
double WeightCalc2 = 15;
double WeightCalc3 = 34;
double WeightCalc4 = 45;
double WeightCalc5 = 60;
double WeightCalc6 = 60;
double priceA = 12;
double priceB = 14;
double priceC = 17;
double priceD = 21;
double priceE = 33;
double priceF = 105;
int basePrice = 12; // declare it here and it will work
//if WeightCalc1 >= packageWeight1 the base charge is 12
if (WeightCalc1 >= packageWeight1)
{
System.out.println("The Base Charge is : " + priceA);
}
Upvotes: 0
Reputation: 159754
basePrice
is defined within the scope of the if
statements. Move this outside the scope of the statement
int basePrice = 12;
if (weightCalc1 >= packageWeight1) {
...
Also the statement
if (perc1 < basePrice < perc2) {
won't compile. Perhaps you meant
if (perc1 < basePrice && basePrice < perc2) {
Upvotes: 0
Reputation: 2715
Declare the int basePrice
at the beginning (with packageWeight1 for example), initialize it to some value (0?) then change all the int basePrice = ...
to basePrice = ...
Once the if statement is finished executing any values declared inside are lost.
Upvotes: 0