I am trying to fix my java application to compile. I am not sure if I made a logical error or what but would really appreciate it if someone could help me out!
*/
import javax.swing.JOptionPane;
public class RentalDialog {
int RentalType = Integer.parseInt(JOptionPane.showInputDialog(null, "Which item would you like to rent?:\n1. Book\n2. Movie\n3. CD"));
double RentalFee = 0;
switch (RentalType) {
case 1 : {
numDays=Integer.parseInt(JOptionPane.showInputDialog("How many days did you want to rent the book at "+bookCOST+ " each:"));
Post the code in code tags and tell us what is wrong with it and we can try to help you. If it doesn't compile post the complete compiler error message(s).
There are a lot of errors in this code, I will list them:
1. You are referencing non static data members in a static way. Either make them static, or instantiate an object of the type RentalDialog and access that objects fields.
2. You have mismatched { }. Fix these. Correctly formatted code will help identify these types of problems.
3. Your case statements fall through. Look up case statements, and the role the "break" keyword plays.
4. DVDCOST is not defined.
5. You have unread variables. Defining total, and then setting it, but never reading it, does no good, and is bad style.
Last edited by DavidFongs; February 17th, 2010 at 03:21 AM.
import javax.swing.JOptionPane;
public class RentalDialog {
double TAX = 0.05;
double bookCOST = 1.50;
double movieCOST = 2.00;
double CDCOST = 1.00;
public static void main(String[] args) {
double taxAmount, subtotal, total;
int numDays;
int RentalType = Integer.parseInt(JOptionPane.showInputDialog(null, "Which item would you like to rent?:\n1. Book\n2. Movie\n3. CD"));
double RentalFee = 0;
switch (RentalType) {
case 1 : {
numDays=Integer.parseInt(JOptionPane.showInputDialog("How many days did you want to rent the book at "+bookCOST+ " each:"));
subtotal = numDays*bookCOST;
taxAmount = subtotal * TAX ;
total = subtotal + taxAmount;
}
}
case 2 : {
numDays=Integer.parseInt(JOptionPane.showInputDialog("How many days did you want to rent the movie at "+movieCOST+ " each:"));
subtotal = numDays*movieCOST;
taxAmount = subtotal * TAX ;
total = subtotal + taxAmount;
}
}
case 3 : {
numDays=Integer.parseInt(JOptionPane.showInputDialog("How many days did you want to rent the DVD at "+DVDCOST+ " each:"));
subtotal = numDays*DVDCOST;
taxAmount = subtotal * TAX ;
total = subtotal + taxAmount;
}
}
Hi, you must evaluate the switch statements to a char,byte, short, int or an enum.
But you are using a double..... Ok
import javax.swing.JOptionPane;
public class Main {
static final double TAX = 0.05;
static final double bookCOST = 1.50;
static final double movieCOST = 2.00;
static final double CDCOST = 1.00;
static final double DVDCOST = 2.00;
public static void main(String[] args) {
double taxAmount, subtotal, total;
int numDays;
int RentalType = Integer.parseInt(JOptionPane.showInputDialog(null,"Which item would you like to rent?:\n1. Book\n2. Movie\n3. CD"));
switch (RentalType) {
case 1:
numDays = Integer
.parseInt(JOptionPane
.showInputDialog("How many days did you want to rent the book at "
+ bookCOST + " each:"));
subtotal = numDays * bookCOST;
taxAmount = subtotal * TAX;
total = subtotal + taxAmount;
break;
case 2:
numDays = Integer
.parseInt(JOptionPane
.showInputDialog("How many days did you want to rent the movie at "
+ movieCOST + " each:"));
subtotal = numDays * movieCOST;
taxAmount = subtotal * TAX;
total = subtotal + taxAmount;
case 3:
numDays = Integer
.parseInt(JOptionPane
.showInputDialog("How many days did you want to rent the DVD at "
+ DVDCOST + " each:"));
subtotal = numDays * DVDCOST;
taxAmount = subtotal * TAX;
total = subtotal + taxAmount;
}
}
}
Try and IDE that helps you to see the errors, I suggest you Eclipse
Bookmarks