• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

my method won't work right

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;
1 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

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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now