Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1038115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:56:05+00:00 2026-05-16T14:56:05+00:00

I’m trying to learn something about GUI, using NetBeans6.8, starting with the GUI section

  • 0

I’m trying to learn something about GUI, using NetBeans6.8, starting with the GUI section in The java tutorial.

There is a simple exercise for a Celsius-Fahrenheit converter. I want that to have two TextFields, one for Celsius and one for Fahrenheit temperature; if the user types in the celsius text field he got the result “printed” in the fahrenheit text filed. and vice versa.

So, i put on both the textfields one KeyTyped event, here’s the code:

private void celsiusTextKeyTyped(java.awt.event.KeyEvent evt) {                                
    int cels = Integer.parseInt(celsiusText.getText());
    int fahr = (int)(cels * 1.8 + 32);
    fahrText.setText(fahr + ""); 
}                                    

private void fahrTextKeyTyped(java.awt.event.KeyEvent evt) {                                
    int fahr = Integer.parseInt(fahrText.getText());
    int cels = (int)(fahr / 1.8 - 32);
    celsiusText.setText(cels + ""); 
}

It doesn’t work. If i type something in a textfield i got this exception: java.lang.NumberFormatException: For input string: ""

The code that attach the listeners:

celsiusText.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyTyped(java.awt.event.KeyEvent evt) {
        celsiusTextKeyTyped(evt);
    }
});

fahrText.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyTyped(java.awt.event.KeyEvent evt) {
        fahrTextKeyTyped(evt);
    }
});

[However, i can’t modify it, it’s autogenerated.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:56:06+00:00Added an answer on May 16, 2026 at 2:56 pm

    Method .getText() returns a string not a number, if that string contains non-numeric characters (i.e. a letter, a space, nothing at all) then parseInt will throw a NumberFormatException. Since your using KeyEvent, as soon as you press say “7”, the event is fired before 7 is entered into the text box. Thus the text box still only contains “”, which is where the error comes from. You may wish to also listen to the keyUp event instead.

    You need to enclose your code in a try catch block.

    private void fahrTextKeyTyped(java.awt.event.KeyEvent evt)
    {   
        try
        {                             
            int fahr = Integer.parseInt(fahrText.getText());
            int cels = (int)(fahr / 1.8 - 32);
            celsiusText.setText(cels + "");
        }
        catch(NumberFormatException ex)
        {
            //Error handling code here, i.e. informative message to the user
        }
    }
    

    An alternative is you could filter out non-numbers on keydown event, see example here – http://www.javacoffeebreak.com/java107/java107.html (Creating a custom component – NumberTextField)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Depends what your host code is. If you end up… May 16, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Found out I could just rebind C-x v = to… May 16, 2026 at 11:42 pm
  • Editorial Team
    Editorial Team added an answer Are you sure you did not mean: Binding b =… May 16, 2026 at 11:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.