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 5 of 5

Thread: Stuck in converting meters in milimeters

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stuck in converting meters in milimeters

    here's my code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class Milimetri extends JFrame {
     
    private JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
    private JLabel e1 = new JLabel("Meters");
    private JLabel e2 = new JLabel("Milimeters:");
     
    private JTextField t1 = new JTextField(20);
    private JTextField t2 = new JTextField(20);
     
    private JButton b1 = new JButton("Result");
     
    public Milimetri(){
    super("Metri in Centimetri");
    setLayout(new GridLayout(3,1));
    add(p1); add(p2); add(p3);
     
    p1.add(e1);p1.add(t1);
    p2.add(e2);p2.add(t2);
    p3.add(b1);
     
    pack();
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
     
    b1.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent ev){
    double meters = Double.parseDouble(t1.getText());
    String milimeters = t2.setText();
     
    }
     
    }
    );
     
    }

    When i write a value in meters ..and press the button b1 to show me a value in milimeters...but i don't know what to do!

    Pls help!

    have a nice day!

    PS: i hope i posted correctly


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Stuck in converting meters in milimeters

    Which part of this is giving you trouble? Can you write a standalone program that simple converts from meters to millimeters using hardcoded double values? Can you write a standalone program that converts a String to a double? Can you write a standalone program that prints out the String from a text component one a gui?

    When you have all of those pieces working separately, then you should start combining them. And if you get stuck on a specific part, you can ask a specific question about that part. Good luck.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Stuck in converting meters in milimeters

    Quote Originally Posted by axeleesh View Post
    String milimeters = t2.setText();
    This is wrong. You have a double value, convert it to String (it's sufficient the string concatenation operator adding to "" or more cleanly the String.valueOf method) and then invoke setText passing that String.

    P.S.
    1) Your code is not well (not at all) indented. Please fix in the post and also in your original code if it is as I see here now.
    2) Please, try to give "good" names to variables, not p1, e1, b1, etc...
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Stuck in converting meters in milimeters

    You had it nearly there, you needed to convert the meters to millimetres by multiplying by 1000, save the result in a double variable.

    Then set the text box value using setText() to the double value in the millimetres variable but you need to convert it back to a string first by using Double.toString().


    Please see my quick mock up below (this works):

    package milimetri;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Milimetri extends JFrame
    {
     
        private JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        private JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        private JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
     
        private JLabel e1 = new JLabel("Meters");
        private JLabel e2 = new JLabel("Milimeters:");
     
        private JTextField t1 = new JTextField(20);
        private JTextField t2 = new JTextField(20);
     
        private JButton b1 = new JButton("Result");
     
        public Milimetri()
        {
            super("Metri in Centimetri");
            setLayout(new GridLayout(3,1));
            add(p1);
            add(p2);
            add(p3);
     
            p1.add(e1);
            p1.add(t1);
            p2.add(e2);
            p2.add(t2);
            p3.add(b1);
     
            pack();
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
     
            b1.addActionListener(new ActionListener()
                                     {
                                        public void actionPerformed(ActionEvent ev)
                                        {
                                            double meters = Double.parseDouble(t1.getText());
                                            double milimeters = meters * 1000;
                                            t2.setText(Double.toString(milimeters));
                                        }
     
                                     }
            );
     
        }
     
        public static void main(String args[])
        {
            Milimetri m = new Milimetri();
        }
     
    }

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stuck in converting meters in milimeters

    Thank you for your help! i understand now what i have to do!

Similar Threads

  1. hi all very new to all this and stuck already....
    By mrgray in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2013, 03:06 PM
  2. Im stuck, please help
    By bigsmoke101 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 12th, 2011, 04:34 PM
  3. PLEASE PLEASE I AM STUCK...
    By ThejavaBUM in forum Loops & Control Statements
    Replies: 2
    Last Post: April 5th, 2011, 10:16 PM
  4. Stuck again
    By Tate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2010, 11:22 AM
  5. I am stuck
    By hawkman4188 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 29th, 2010, 12:46 AM