CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    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;
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2009
    Posts
    17

    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 03:21 AM.

  4. #4
    Join Date
    Feb 2010
    Posts
    1

    Cool 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

  5. #5
    Join Date
    Mar 2010
    Location
    Mexico
    Posts
    9

    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

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width