|
-
February 15th, 2010, 07:04 PM
#1
Basic Java Programming Question
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 {
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;
}
}
-
February 16th, 2010, 12:04 PM
#2
Re: Basic Java Programming Question
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).
-
February 17th, 2010, 04:19 AM
#3
Re: Basic Java Programming Question
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 04:21 AM.
-
February 18th, 2010, 02:37 PM
#4
Re: Basic Java Programming Question
Code:
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
-
March 5th, 2010, 05:12 PM
#5
Re: Basic Java Programming Question
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|