Results 1 to 4 of 4

Thread: Help with Java Program

  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Help with Java Program

    All, I am new to this forum and I am hoping I came to the right spot.... I am having trouble with this simple program. What I am attempting to do is simply work with dialog boxes and user inputs. My end state for this current segment is to simply have this output happen when I run the program:

    Dialog Box: Enter the customer's package (A, B, or C);

    User input: A

    Dialog Box: Enter the hours used:

    Any overages in hours need to be added as additional charges. For example Package A has a maximum of 10 hours access. Additional hours are $2.00 per hour. So if a user input 11 hours for Package his total would be $11.95. It needs to be adjustable of course with user input for hours. I am not sure how to do this? Any input would be greatly appreciated.

    Dialog Box/Message: The charges are "total with hours input."


    Thank you in advance


    Java Code:
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;


    public class ColapietroPass4Draft
    {

       public static void main(String[] args)
       
       {
               
                char packageSelect;
                final double Package_A = 9.95,                      
                             Package_B = 13.95,
                             Package_C = 19.95,
                             Package_A_Hours = 10,
                             Package_B_Hours = 20;

                double hoursSelect;
                                       
               
                String input = JOptionPane.showInputDialog("Enter customer's package (A,B or C):");
                System.out.println();
                packageSelect = input.charAt(0);
               
               
                String hours = JOptionPane.showInputDialog("Enter the number of hours used):");

               
                if (packageSelect == 'A')
                {
                 JOptionPane.showMessageDialog(null, "The charges are " + Package_A);
                 }
                 
                if (packageSelect == 'B')
                {
                   JOptionPane.showMessageDialog(null, "The charges are " + Package_B);
                   }
                   
                if (packageSelect == 'C')
                {
                   JOptionPane.showMessageDialog(null, "The charges are " + Package_C);          
                   }
                   
                         
              System.exit(0);        
               
               
                  }

    }
    Last edited by cal516; 03-18-2015 at 01:20 AM. Reason: Updating

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Problem is you're mixing Swing with Scanner. Use one or the other but not both.


    Java Code:
    public class Hmk {

        /**
         * @param args the command line arguments
         */

        public static void main(String[] args) {
            String res = JOptionPane.showInputDialog("Enter Package:");
            if (res != null && !res.trim().isEmpty()) {
                if (res.equals("A")) {
                    JOptionPane.showMessageDialog(null, "9.95", "Price: ", JOptionPane.INFORMATION_MESSAGE);
                }
            } else {
                //invalid input.. user entered nothing.. do something here..
            }
        }
    }


    Also, to highlight code on the forum, use the Highlight tags:

    Code:
    [Highlight=Java]
        Put your java code here..
    [/Highlight]
    Last edited by Brandon; 03-17-2015 at 07:24 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Aug 2014
    Posts
    25
    Mentioned
    0 Post(s)
    Quoted
    11 Post(s)

    Default

    Hey, a Scanner is for the console input. The joptionPane is a standard gui. Both are used to get a user input. It's up to you now which one you want to use. Please use the code tags next time. For java: [Highlight=Java ] and [/ Highlight] without spaces
    Java Code:
    import javax.swing.JOptionPane;

    public class MonthlyDraft {

        public static void main(String[] args) {

            char packageSelect;
            final double Package_A = 9.95;

            String input = JOptionPane
                    .showInputDialog("Enter customer's package (A, B or C):");

            System.out.println("You entered: " + input); // to debug the input
            packageSelect = input.charAt(0);
            if (packageSelect == 'A') {
                JOptionPane.showMessageDialog(null, "The charges are " + Package_A);
            }
            System.exit(0);

        }

    }

  4. #4
    Join Date
    Mar 2015
    Posts
    2
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Great help!!! Thank you...I was able to work it out.....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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