Number from text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fifersheep
    New Member
    • Apr 2008
    • 3

    Number from text box

    Hi, there, very new to Java. I'm looking to get a number from a text box, then use the number in a calculation, and then put the answer into another text box.

    JButton convertButton = new JButton ("Convert to Fahrenheit");
    final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
    final JTextField fahrenheitText = new JTextField (" ");

    JPanel buttons = new JPanel();
    buttons.setLayo ut (new FlowLayout());

    buttons.add (convertButton) ;

    setTitle("Doubl er Frame");
    setLayout(new BorderLayout()) ;
    add(fahrenheitT ext, BorderLayout.SO UTH);
    add(buttons, BorderLayout.CE NTER);
    add(celsiusText , BorderLayout.NO RTH);

    convertButton.a ddActionListene r (new ActionListener( )
    {
    public void actionPerformed ( ActionEvent e)
    {

    fahrenheit = (9/5)* + 33.8;
    fahrenheitText. setText(Tempera ture in Fahrenheit: " + fahrenheit);
    }
    });

    This is the main part of the code, I'm very confused and can't figure out whats wrong.

    Hope you guys can spot my mistake.

    I get an answer of 33.8, no matter what I enter in the first text box.

    Would much appreciate help. Thanks!
    Last edited by fifersheep; Apr 9 '08, 12:10 AM. Reason: Long post, added bold and italics to show code clearly
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by fifersheep
    Hi, there, very new to Java. I'm looking to get a number from a text box, then use the number in a calculation, and then put the answer into another text box.

    JButton convertButton = new JButton ("Convert to Fahrenheit");
    final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
    final JTextField fahrenheitText = new JTextField (" ");

    JPanel buttons = new JPanel();
    buttons.setLayo ut (new FlowLayout());

    buttons.add (convertButton) ;

    setTitle("Doubl er Frame");
    setLayout(new BorderLayout()) ;
    add(fahrenheitT ext, BorderLayout.SO UTH);
    add(buttons, BorderLayout.CE NTER);
    add(celsiusText , BorderLayout.NO RTH);

    convertButton.a ddActionListene r (new ActionListener( )
    {
    public void actionPerformed ( ActionEvent e)
    {

    fahrenheit = (9/5)* + 33.8;
    fahrenheitText. setText(Tempera ture in Fahrenheit: " + fahrenheit);
    }
    });

    This is the main part of the code, I'm very confused and can't figure out whats wrong.

    Hope you guys can spot my mistake.

    I get an answer of 33.8, no matter what I enter in the first text box.

    Would much appreciate help. Thanks!
    If (9/5) is A
    and 33.8 is B

    the formula is A * celsiusvalue + B
    since celsiusvalue is 0... A * celsiusvalue always returns 0...
    That is why 33.8.....

    1. Please post your code with codetags.
    2. Someone may attempt to test your code, but your code is not complete....
    3. You forgot to set the size of the frame.
    4. fahrenheit is what Data type?
    5. Having the input String to Double
    - Get the String value of celsiusText
    - Use Double.parseDou ble(String) for Double Conversion.... or float
    - Add the value into your formula....
    6. Download the Java Core API... It is useful...
    7. Good luck.

    sukatoa...

    Comment

    • fifersheep
      New Member
      • Apr 2008
      • 3

      #3
      Ok this is my code in full now:

      import javax.swing.*;
      import javax.swing.JOp tionPane;
      import java.awt.*;
      import java.awt.event. *;

      public class Temperature {

      public static void main(String[] args) {

      tempConvert buttonFrame = new tempConvert();
      buttonFrame.set Size(350,120);
      buttonFrame.set Location(250,10 0);
      buttonFrame.set Visible(true);
      }
      }

      class tempConvert extends JFrame{

      //Stores default temperature numbers
      double celsius = 1;
      double fahrenheit = 33.8;

      public tempConvert ()
      {
      JButton convertButton = new JButton ("Convert to Fahrenheit");
      final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
      final JTextField fahrenheitText = new JTextField (" ");

      JPanel buttons = new JPanel();
      buttons.setLayo ut (new FlowLayout());

      buttons.add (convertButton) ;

      //creates layout of window
      setTitle("Doubl er Frame");
      setLayout(new BorderLayout()) ;
      add(fahrenheitT ext, BorderLayout.SO UTH);
      add(buttons, BorderLayout.CE NTER);
      add(celsiusText , BorderLayout.NO RTH);

      //states what to do when button is pressed
      convertButton.a ddActionListene r (new ActionListener( )
      {
      public void actionPerformed ( ActionEvent e)
      {
      celsius = Double.parseDou ble (celsiusText);
      fahrenheit = (9/5)* + 33.8;
      fahrenheitText. setText("Fahren heit is: " + fahrenheit);
      }
      });
      }
      }


      and I still get "Error: parseDouble(jav a.lang.String) in java.lang.Doubl e cannot be applied to (javax.swing.JT extField)"

      I don't know if I'm using the parse correctly...
      Last edited by fifersheep; Apr 9 '08, 01:34 AM. Reason: a few typo's

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by fifersheep
        Ok this is my code in full now:

        import javax.swing.*;
        import javax.swing.JOp tionPane;
        import java.awt.*;
        import java.awt.event. *;

        public class Temperature {

        public static void main(String[] args) {

        tempConvert buttonFrame = new tempConvert();
        buttonFrame.set Size(350,120);
        buttonFrame.set Location(250,10 0);
        buttonFrame.set Visible(true);
        }
        }

        class tempConvert extends JFrame{

        //Stores default temperature numbers
        double celsius = 1;
        double fahrenheit = 33.8;

        public tempConvert ()
        {
        JButton convertButton = new JButton ("Convert to Fahrenheit");
        final JTextField celsiusText = new JTextField ("Enter temperature in Celsius to convert");
        final JTextField fahrenheitText = new JTextField (" ");

        JPanel buttons = new JPanel();
        buttons.setLayo ut (new FlowLayout());

        buttons.add (convertButton) ;

        //creates layout of window
        setTitle("Doubl er Frame");
        setLayout(new BorderLayout()) ;
        add(fahrenheitT ext, BorderLayout.SO UTH);
        add(buttons, BorderLayout.CE NTER);
        add(celsiusText , BorderLayout.NO RTH);

        //states what to do when button is pressed
        convertButton.a ddActionListene r (new ActionListener( )
        {
        public void actionPerformed ( ActionEvent e)
        {
        celsius = Double.parseDou ble (celsiusText);
        fahrenheit = (9/5)* + 33.8;
        fahrenheitText. setText("Fahren heit is: " + fahrenheit);
        }
        });
        }
        }


        and I still get "Error: parseDouble(jav a.lang.String) in java.lang.Doubl e cannot be applied to (javax.swing.JT extField)"

        I don't know if I'm using the parse correctly...
        parseDouble needs a parameter String....
        That string maybe the string value of textfield......
        textfield.getTe xt().... read about it....

        That String will be used to convert the String into Double....
        That Double will then be returned after the process.....

        Where must the celsius be on the formula? hint...

        The complete explaination is on the web....
        you can google it.

        sukatoa

        Comment

        • fifersheep
          New Member
          • Apr 2008
          • 3

          #5
          Originally posted by sukatoa
          parseDouble needs a parameter String....
          That string maybe the string value of textfield......
          textfield.getTe xt().... read about it....

          That String will be used to convert the String into Double....
          That Double will then be returned after the process.....

          Where must the celsius be on the formula? hint...

          The complete explaination is on the web....
          you can google it.

          sukatoa

          hmmm... still cant get the String from the textbox, doesn't matter though mate, might just try it using an input box or something, I'm sure I'll make sense of it all on day, and thanks for the help anyway man, appreciate it!

          Take care

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            That constant 33.8 in your (linear) conversion formula is incorrect:

            F = 9/5*C+32
            C = 5/9*(F-32)

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              That constant 33.8 in your (linear) conversion formula is incorrect:

              F = 9/5*C+32
              C = 5/9*(F-32)

              kind regards,

              Jos
              I've given up trying to guess where the OP got 33.8 from.

              Comment

              Working...