Can't declare Variable in new method
In the very last method below
Code Java:
import javax.swing.JOptionPane;
public class program08 {
public static void main(String[] args) {
String getWeightString = JOptionPane.showInputDialog(null,
"Enter the weight of the package");
double getWeight = Double.parseDouble(getWeightString);
String handling = JOptionPane.showInputDialog(null,
"Would you like Special handling? Enter 'Y' for yes.");
char handlingChar = handling.charAt(0);
String insurance = JOptionPane.showInputDialog (null,
"Would you like insurance? Enter 'Y' for yes.");
char insuranceChar = insurance.charAt(0);
JOptionPane.showMessageDialog(null, "The total cost is" + "");
}
public static double price(double getWeight) {
double price;
if (getWeight <= 2.00)
price = 1.00;
else if (getWeight > 2.00 && getWeight < 5.00)
price = 2.00;
else
price = 4.00;
return price;
}
public static double handlingPrice(char handlingChar) {
double handlingPrice =0;
if (handlingChar == 'Y' || handlingChar == 'y')
handlingPrice = 5.00;
return handlingPrice;
}
public static double insPrice(char insuranceChar) {
double insPrice = 0;
if (insuranceChar == 'Y' || insuranceChar == 'y')
String insPriceString = JOptionPane.showInputDialog(null,
"How much insurance would you like?");
insPrice = Double.parseDouble(insPriceString);
insPrice += insPrice * 0.10;
return insPrice;
}
}
At the line that reads "String insPriceString = JOptionPane.showInputDialog"
I get a warning that reads "Variable declaration not allowed here"
The assignment requires that the variable for the amount of insurance the user wants, if they want insurance, to be declared in a different method, not the main method.
Re: Can't declare Variable in new method
You need to add {}s to enclose the statements controlled by the if statement.
Re: Can't declare Variable in new method
The if statement is not a loop. In only needs {} if it is a loop, no?
Re: Can't declare Variable in new method
you have to add braces in your condition
and concentrate with your return statement..
Learn this: coding style - Single statement if block - braces or no? - Programmers
Re: Can't declare Variable in new method
You should always code {}s with if, else and loops