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

Thread: Can't put decimals in my textfields?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't put decimals in my textfields?

    I have this chunk of code. It's a simple BMI calculator, but I've noticed that I can only enter whole numbers into my textfields. What can I do to allow decimals?

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class BMICALC extends Applet implements ActionListener
    {
    	int in, lbs;
    	double m, kg, bmi;
     
    	Label titleLabel = new Label ("Body Mass Index Calculator");
    	//
    	Label hLabel = new Label ("Enter your height in inches: ");
    	TextField hField = new TextField(10);
    	//
    	Label wLabel = new Label ("Enter your weight in pounds: ");
    	TextField wField = new TextField(10);
    	//
    	Button bmiButton = new Button("Calculate");
    	Label oLabel = new Label ("Click Calculate to see your body mass index.");
     
    	public void init()
    	{
    		setForeground(Color.white);
    		setBackground(Color.black);
    		add(titleLabel);
    		add(hLabel);
    		setForeground(Color.black);
    		add(hField);
    		setForeground(Color.white);
    		add(wLabel);
    		setForeground(Color.black);
    		add(wField);
    		add(bmiButton);
    		setForeground(Color.white);
    		bmiButton.addActionListener(this);
    		setForeground(Color.green);
    		add(oLabel);
    	}
     
     
    	public void actionPerformed(ActionEvent k)
    	{
     
    		in = Integer.parseInt(hField.getText());
    		lbs = Integer.parseInt(wField.getText());
    		m = in / 39.36;
    		kg = lbs / 2.2;
    		bmi = kg / Math.pow(m,2);
    		oLabel.setText("Your body mass index is " + Math.round(bmi) + ".");
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Can't put decimals in my textfields?

    An integer is a non decimal number, so you might want to parse the input to a different type of number.

    Rolf

  3. #3
    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: Can't put decimals in my textfields?

    See the Double and Float classes for other parse methods that take decimal numbers.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. mulitiple textfields help
    By derekxec in forum AWT / Java Swing
    Replies: 1
    Last Post: December 10th, 2011, 09:05 PM
  2. [SOLVED] Processing Decimals <1 & >0
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2011, 01:53 PM
  3. labels,textfields,buttons
    By balu424525 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 26th, 2011, 07:44 AM
  4. Convert Decimals to Octals using only If/Else
    By jcattau in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 9th, 2011, 12:58 PM
  5. Exception handling for TextFields
    By FretDancer69 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 16th, 2009, 07:48 AM