my method won't work right

Posted on 2005-04-19
Last Modified: 2010-03-31
my calculate method is acting up ... for example i can't use the fahrenheit conversion proberly... when I click nothing happens... what did i do wrong ?

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

public class calc{
      public static void main (String [] args){
            screen go = new screen();

class screen extends JFrame {
      //The overall Frame
      public screen(){
            setTitle("conversion calculator");
            setSize(WIDTH, HEIGHT);
      //declaring the number block      
            buttons panel = new buttons();

      //the Content screen
            Container contentPane = getContentPane();

      public static final int WIDTH = 800;
      public static final int HEIGHT = 200;

class buttons extends JPanel{
      public buttons(){
            setLayout(new BorderLayout());
            result = 0;
            lastCommand = "=";
            start = true;
            // display the text field
            display = new JTextField("0");
            add(display, BorderLayout.CENTER);
            ActionListener insert = new insertAction();
            ActionListener command = new CommandAction();
            // conversion function layout
            panel = new JPanel();
            panel.setLayout(new GridLayout(4,3));
            addButton("Celsius", command);
            addButton("In", insert);
            addButton("mm", insert);
            addButton("Fahrenheit", command);
            addButton("Ft", insert);
            addButton("cm", insert);
            addButton("Clear", command);
            addButton("Yd", insert);
            addButton("m", insert);
            addButton("Convert", command);
            addButton("Mi", insert);
            addButton("km", insert);
            add(panel, BorderLayout.EAST);
            // grid layout
            panel = new JPanel();
            panel.setLayout(new GridLayout(4,4));
            addButton("7", insert);
            addButton("8", insert);
            addButton("9", insert);
            addButton("/", command);
            addButton("4", insert);
            addButton("5", insert);
            addButton("6", insert);
            addButton("*", command);
            addButton("1", insert);
            addButton("2", insert);
            addButton("3", insert);
            addButton("-", command);
            addButton("0", insert);
            addButton(".", insert);
            addButton("=", command);
            addButton("+", command);
            add(panel, BorderLayout.WEST);
      private void addButton(String label, ActionListener listener){
            JButton button = new JButton(label);
      private class insertAction implements ActionListener{
            public void actionPerformed(ActionEvent event){
                  String input = event.getActionCommand();
                  if (start){
                        start = false;
                  display.setText(display.getText() + input);
      private class CommandAction implements ActionListener{
            public void actionPerformed(ActionEvent evt){
                  String command = evt.getActionCommand();
                  if (start){
                        if (command.equals("-")){
                              start = false;
                              lastCommand = command;
                        lastCommand = command;
                        start = true;
      public void calculate(double x){
            if (lastCommand.equals("+")) result += x;
            else if (lastCommand.equals("-")) result -= x;
            else if (lastCommand.equals("*")) result *= x;
            else if (lastCommand.equals("/")) result /= x;
            else if (lastCommand.equals("=")) result = x;
            else if (lastCommand.equals("Celsius")) result = (5 / 9) * (x - 32);
            else if (lastCommand.equals("Fahrenheit")) result = (9 / 5) * (x + 32);
            else if (lastCommand.equals("Clear")) result = 0;
            display.setText("" + result);
      private JTextField display;
      private JPanel panel;
      private JPanel panel2;
      private double result;
      private String lastCommand;
      private boolean start;
Question by:vipa2k
    LVL 3

    Accepted Solution

    There are two main issue.
    The first one is that you cannot do the conversion like this:
    result = (5 / 9) * (x - 32);

    5 and 9 are intrepeted as Int, so you get a Int as result, which is 0 in this case, beaing 5 smallr than 9.
    You need to be more explicit about the kind of data:

    result = (5.0 / 9.0) * (x - 32.0);
    or better
    result = (5d / 9d) * (x - 32d); //The d stand for double (you can use f for float too)

    The second issue is that the operation you have to press Celsius before to enter the number to convert, ans then press =, like this:
    "Celsius" 100 =

    I dont know if that is exacly what you want. But maybe is not. In that case you need to work on the design concept of your work a little more.

    Hope i've been of help

    LVL 1

    Expert Comment

    To follow on from the previous response I think the correct formula for Celsius / Fahrenheit conversions is actually:

    result = (9.0d / 5.0d * x) + 32.0d;

    that is the addition part is done after the other parts of the conversion.

    Hope that is some help.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    withoutTen challenge 14 69
    dividesSelf challange 15 60
    wordsWithout 49 64
    Updating Java 9 62
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now