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: What am I doing wrong?

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question What am I doing wrong?

    Hi, can someone help we please?
    I need to write a temperature-conversion application that converts from fahrenheit to celsius. The temperature should be entered from the keyboard(via a jtextfield). And a label should be used to display the converted temperature.

    This is mine code:

    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Conversion extends JFrame{
        private final JTextField fField;
        private final JLabel cLabel;
        private final JLabel label;
        private final JLabel fLabel;
        private final JPanel panel;
        private final JPanel fPanel;
        private final JPanel cPanel;
     
     
        public Conversion(){
            super("Temperature Conversion");
            setLayout(new FlowLayout());
     
            panel = new JPanel(new GridLayout(2,1));
            fPanel = new JPanel(new GridLayout(1,2));
            cPanel = new JPanel(new GridLayout(1,2));
     
            fLabel = new JLabel("Enter fahrenheit: ");
            fField = new JTextField(5);
            fPanel.add(fLabel);
            fPanel.add(fField);
     
            int f = Integer.parseInt(fField.getText());
            int c = 5/9 * (f - 32);
     
            label = new JLabel();
            label.setText(Integer.toString(c));
            cLabel = new JLabel("Celsius: ");
            cPanel.add(cLabel);
            cPanel.add(label);
     
            panel.add(fPanel);
            panel.add(cPanel);
     
            add(panel);
        }
    }

    This is the error i'm getting:
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at Conversion.<init>(Conversion.java:32)
    at ConversionTest.main(ConversionTest.java:5)
    C:\Users\Maneesha\AppData\Local\NetBeans\Cache\10. 0\executor-snippets\run.xml:111: The following error occurred while executing this line:
    C:\Users\Maneesha\AppData\Local\NetBeans\Cache\10. 0\executor-snippets\run.xml:68: Java returned: 1

  2. #2
    Junior Member
    Join Date
    Apr 2019
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What am I doing wrong?

    it's still not working

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: What am I doing wrong?

    First, all you have provided is the constructor. You also need a static main method as an entry point (or include the rest of your code that creates an instance of the Conversion class).

    You need to set up your Frame properly and add your text field to it. Then display the frame. I recommend you check out the tutorials in my signature on how to use JTextFields. They give some good examples.

    Also, it is considered bad practice to extend JFrame. Best to just create an instance and use that.

    Regards,
    Jim

  4. The Following 2 Users Say Thank You to jim829 For This Useful Post:

    Faerthurin (April 9th, 2019), John Joe (April 9th, 2019)

  5. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: What am I doing wrong?

    fField.getText().toString(); always return empty string, that why you will get exception. If you want to get value from textField, I would suggest you to add a button there . Once the button clicked, get the value and display on JLabel.
    Last edited by John Joe; April 9th, 2019 at 12:34 PM.
    Whatever you are, be a good one

  6. The Following User Says Thank You to John Joe For This Useful Post:

    Faerthurin (April 9th, 2019)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What am I doing wrong?

    Using a GUI requires that the code present a window with text fields, etc and then wait for user interaction. The JVM handles user events and will pass control to a listener when a particular event, like a button press, happens.

    The sequence of events would be:
    The program builds and displays the GUI and sets a listener
    and then exit sthe method to wait for user input
    The user enters some data and presses a button
    The JVM sees the button press and calls the listener for the button
    the listener then gets the data that the user entered and processes it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is wrong?
    By iMiTzi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 10th, 2014, 08:48 AM
  2. What is wrong?
    By iMiTzi in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 10th, 2014, 06:11 AM
  3. Replies: 4
    Last Post: March 6th, 2014, 05:14 PM
  4. [SOLVED] What am i doing wrong?
    By Inguana in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2013, 11:13 AM
  5. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM