Results 1 to 5 of 5
  1. #1
    Zyrion is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Java Assignment (If block)

    My professor gave our class an assignment where the user needs to enter a value, between 5 cents and a dollar. I cant get my conditional expressions to work correctly in my if-block. Here is what I have done so far.

    It does compile without any syntax errors.

    EDIT: I figured out the problem. Sense I changed everything to a multiple of 100 I forgot to remove the * 100 next to the parse method call. I am still open to criticism on my work, it would only help me.

    #2EDIT:Now I have encountered a new issue with my updated code. I am attempting to concat strings and print them using JOptionPane,
    but the string wont print and is left blank. I need advice on this issue, updated code below.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class VendingMachine 
    {
        public static void main( String [] args)
        {
            final int NICKEL, DIME, QUARTER, DOLLAR;
            NICKEL = 05;
            DIME = 10;
            QUARTER = 25;
            DOLLAR = 100;
            
            int User_Input_Cast, div, mod; 
      
            String FinalReceipt = "Your change is: ";
            String User_Input = JOptionPane.showInputDialog("Enter an price between 05 and 100");
            User_Input_Cast = Integer.parseInt(User_Input);
            
            /*Check if input is a multiple of 5, this ensures that a price  
             * is entered correctly
             */
            if( (User_Input_Cast % NICKEL) == 0 && User_Input_Cast <= 100 && User_Input_Cast >= 5) 
             {   //we fconfigure qurater amount here
               div = User_Input_Cast / QUARTER;
               mod = User_Input_Cast % QUARTER;
               FinalReceipt.concat(div + " Quarters, ");
                    if(mod != 0)
                    { //we configure dime amount here
                        div = mod / DIME;
                        mod = mod % DIME;
                        FinalReceipt.concat(div + " Dimes, ");
                            if(mod != 0)
                            {
                                div = mod / NICKEL;
                                mod = mod % NICKEL;
                                FinalReceipt.concat(div + " NICKEL, ");
                                JOptionPane.showMessageDialog(null,
                                                        FinalReceipt,                        //Does not print this string
                                                       "Receipt Final",
                                                       JOptionPane.PLAIN_MESSAGE);
                            }
                            else{ //0 nickels
                              JOptionPane.showMessageDialog(null,
                                                     FinalReceipt,                           //Does not print this string
                                                    "Receipt no nickels",
                                                    JOptionPane.PLAIN_MESSAGE);
                            }//end of third tier if-block
                    }
                    else{ //0 dimes
                       JOptionPane.showMessageDialog(null,
                                               FinalReceipt,                                 //Does not print this string
                                              "Receipt no dimes",
                                              JOptionPane.PLAIN_MESSAGE);
                    }//end of second tier if-block
             }
            else{
               JOptionPane.showMessageDialog( null,
                                     "The number you entered was incorrect: " + User_Input_Cast,
                                     "Incorrect Input",
                                      JOptionPane.ERROR_MESSAGE);
             }//end of if block  
                                            
                                                
        }//end of main 
    }//end of class
    Last edited by Zyrion; 02-08-2013 at 02:06 AM.

  2. #2
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Java Assignment (If block)

    It looks good, as a Newbie myself I have been told to lean towards camel coding.

    so userInputParse instead of User_Input_Parse

  3. #3
    Zyrion is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Java Assignment (If block)

    Thanks for the advice :)

  4. #4
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Java Assignment (If block)

    Hey mate I think you've done some pretty stuff, however if there's anything to add to your code it would be handling an exception with a try...catch

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Java Assignment (If block)

    Hi Zyrion,

    Your concat is not working as you expected, you still need to assign the result of the concat back to the variable.
    Java Code:
    FinalReceipt = FinalReceipt.concat(div + " Quarters, ");
    Regards.

Similar Threads

  1. doubt on synchronized block in java
    By me_shankara in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-20-2012, 01:38 PM
  2. Synchronized block in java
    By me_shankara in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-12-2012, 01:34 PM
  3. java program to block a web browser or change proxy setting...
    By tafadzwa joseph in forum New To Java
    Replies: 1
    Last Post: 06-25-2012, 05:31 PM
  4. java skips a block of code...
    By JavaSal in forum New To Java
    Replies: 8
    Last Post: 02-07-2010, 10:58 AM
  5. Replies: 1
    Last Post: 10-13-2009, 08:31 AM

Posting Permissions

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