Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: cents to dollars and cents program

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cents to dollars and cents program

    Hello everyone. I just have a quick question about a program that would make my life easier. I want to create a program that gives the user a textbox where they can input cents. I would like to do some sort of validating to make sure that numbers are entered for cents. Then the program breaks down the cents into dollars and cents and displays it back to the user using a label. Any suggestions or pointers on how to begin would be greatly appreciated.

    Thanks,
    Roaster


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: cents to dollars and cents program

    What exactly do you need help with, the code to display the input box or the actual calculation of cents do dollars?

    For UI stuff, check out Swing if you wish to do this as a normal application or servlets/jsp if you wish to do this as a webpage.

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Errors

    Hello, I am trying to create a program that allows a user to input cents into a textfield and then the program converts the cents to dollars and cents. I have a good bit of it written I think/hope. Anyways I need some help on some errors that I do not know how to fix.

    My first error in this code when I go to compile is right after the comment:
    //Add the panel to the frame
    the error says that it cannot find symbol - method add(NamePanel)
    any suggestions

    import javax.swing.*;
     
    public class MoneyPanel
    {
       public static void main(String [] args)
       {
        //Create a frame
        JFrame frame = new JFrame("Name Example");
        //End the program when the frame is closed
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Create a NamePanel
        NamePanel panel = new NamePanel();
        //Add the panel to the frame
        frame.getContentPane().add(panel);
        //Set the size of the frame
        frame.pack();
        //Make it visible
        frame.setVisible(true);
        }
    }

    Now for the other part of the program. I get an error right after the comment:
    //Add components to this panel
    The error says "cannot find symbol - method add(javax.swing.JTextField)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import static java.lang.System.out;
     
    public class NamePanel
    {
        //Constants known through all of the class
        private final int PANEL_HEIGHT = 100;
        private final int PANEL_WIDTH = 250;
        private final Color BACK_COLOR = Color.ORANGE;
     
        //Text field for cents to be input
        private JTextField text;
     
        //Label to show cents converted to dollars and cents
        private JLabel Solution;
     
        /**
         *  Constructor for NamePanel initializes the text field
         *  several labels and such
         */
        public NamePanel()
        {
        //Initialize text area to be 10 characters wide
        text = new JTextField(10);
     
        //Register a listener
        text.addActionListener(new MyTextListener());
     
        //Initialize the label
        Solution = new JLabel("");
     
        //Add components to this panel
        add(text);
        add(Solution);
     
        //Set the size
        setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
     
        //Set the color
        setBackground(BACK_COLOR);
        }
     
        //Internal Listener with access to the variables
        private class MyTextListener implements ActionListener
        {
        public void actionPerformed(ActionEvent event)
            {
                //Read in the string typed by the user
                String totalCents = text.getText();
     
                //Parse the string into Int
                int value = Integer.parseInt(totalCents);
     
                //Converts cents to dollars and cents
                int dollars = value  / 100;
                int cents = value % 100;
     
                //Setting the text shown by labels
     
                Solution.setText("The cents entered equal: $" + dollars + "." + cents);
            }
        }
    }


    Any help/suggestions would be greatly appreciated.

    Thank You,
    roaster
    Last edited by helloworld922; October 22nd, 2009 at 08:57 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: cents to dollars and cents program

    I believe it's not letting you add because your NamePanel class doesn't extend Component (or some other class).