Welcome to Dream.In.Code
Become a Java Expert!

Join 150,126 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,018 people online right now. Registration is fast and FREE... Join Now!




Possible Loss of precision error

 
Reply to this topicStart new topic

Possible Loss of precision error

MBAStudent
25 Jun, 2008 - 08:26 AM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 2

Ok - I'm at a loss - I keep fixing errors in this code and it just keeps generating more errors. Hope someone out there would be kind enough to take a look at it and maybe explain to me what I'm doing wrong. Please keep in mind - I'm a total beginner and I have to keep looking things up to understand what I'm doing. My code below is suppose to generate a calculator that will allow the user to input decimal numbers. THANKS IN ADVANCE !!

CODE


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

public class Calculator extends JFrame implements ActionListener
{  
    private JTextField displayText = new JTextField(30);
    private JButton[] button = new JButton[16];

    private String[] keys = {"7", "8", "9", "/",
                             "4", "5", "6", "*",
                             "1", "2", "3", "-",
                             "0", "C", "=", "+"};

    private String numStr1 = "";
    private String numStr2 = "";

    private char op;
    private boolean firstInput = true;

    public String Calculator()
{  
       setTitle("My Calulator");
       setSize(230, 200);
       Container pane = getContentPane();
      
       pane.setLayout(null);

       displayText.setSize(200, 30);
       displayText.setLocation(10, 10);
       pane.add(displayText);

       int x, y;

       x = 10;
       y = 40;
       for (int ind = 0; ind < 16; ind++)
       {
           button[ind] = new JButton(keys[ind]);
           button[ind].addActionListener(this);
           button[ind].setSize(50, 30);
           button[ind].setLocation(x, y);
           pane.add(button[ind]);
           x = x + 50;
           if ((ind + 1) % 4 == 0)
           {
                 x = 10;
                 y = y + 30;
           }
    }
    
    this.addWindowListener(new WindowAdapter()
           {
               public void windowClosing(WindowEvent e)
               {
                      System.exit(0);
               }
           }
    );

    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
    String resultStr;

    String str
       = String.valueOf(e.getActionCommand());

    char ch = str.charAt(0);

    switch (ch)
    {
    case '0': case '1': case '2':
    case '3': case '4': case '5':
    case '6': case '7': case '8':
    case '9': if (firstInput)
             {
                 numStr1 = numStr1 + ch;
                 displayText.setText(numStr1);
             }
             else
             {
                 numStr2 = numStr2 + ch;
                 displayText.setText(numStr2);
             }
             break;
    case '+': case '-': case '*':
    case '/': op = ch;
              firstInput = false;
              break;
    case '=': resultStr = evaluate();
              displayText.setText(resultStr);
              numStr1 = resultStr;
              numStr2 = "";
              firstInput = false;
              break;
    case 'C': displayText.setText("");
              numStr1 = "";
              numStr2 = "";
              firstInput = true;
    }
}
private String evaluate()
   {
   final char beep = '\u0007';

   try
   {
       int num1 = Double.parseDouble(num1Str1);
       int num2 = Double.parseDouble(num1Str2);
       int result = 0;

       switch (op)
       {
       case '+': result = num1 + num2;
                 break;
       case '-': result = num1 - num2;
                 break;
       case '*': result = num1 * num2;
                 break;
       case '/': result = num1 / num2;
       }

       return String.valueOf(result);
    }
    catch (ArithmeticException e)
    {
          System.out.print(beep);
          return "E R R O R: " + e.getMessage();
    }
    catch (NumberFormatException e)
    {
          System.out.print(beep);
          if (numStr1.equals(""))
              return "E R R O R: Invalid First Number";
          else
              return "E R R O R: Invalid Second Number";
    }
    catch (Exception e)
    {
          System.out.print(beep);
          return "E R R O R";
    }
}  
public static void main(String[] args)
  {
    Calculator c = new Calculator();
  }
}


Here are the latest error messages I get when I try to compile:

java: 122: cannot resolve symbol
symbol : variable num1Str1
location: class Calculator
int num1 = Double.parseDouble(num1Str1);

java:122: possible loss of precision
found : double
required: int
int num1 = Double.parseDouble(num1Str1);
^
java:123: cannot resolve symbol
symbol : variable num1Str2
location: class A6ST3155687
int num2 = Double.parseDouble(num1Str2);
^
java:123: possible loss of precision
found : double
required: int
int num2 = Double.parseDouble(num1Str2);
^
java:160: cannot resolve symbol
symbol : class Calculator
location: class A6ST3155687
Calculator c = new Calculator();
^
java:160: cannot resolve symbol
symbol : class Calculator
location: class A6ST3155687
Calculator c = new Calculator();
^
6 errors

^


This post has been edited by MBAStudent: 25 Jun, 2008 - 08:30 AM
User is offlineProfile CardPM
+Quote Post

Locke37
RE: Possible Loss Of Precision Error
25 Jun, 2008 - 09:17 AM
Post #2

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,274



Thanked: 57 times
Dream Kudos: 325
My Contributions
Well, I know that the last 2 errors, have to deal with the contructor. You didn't actually make a constructor.

You did this.

java
public String Calculator()


You need to remove the String.

EDIT: I have solutions for the other 2 types of errors, keep reading. icon_up.gif
_______________________________________________

The loss of precision errors, are because you have declared the num1 and num2 as integers, all you need to do is change those to doubles.
_______________________________________________

The other 2, where it can't find the string variables, are because you did not name the variables num1Str1 and num1Str2. Simply take out the '1' after 'num'.

That should take care of them all. smile.gif

This post has been edited by Locke37: 25 Jun, 2008 - 09:18 AM
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Possible Loss Of Precision Error
25 Jun, 2008 - 09:24 AM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
ok here are some issues


You are casting a double to an int. Cant do that. Change num2 and num1 like so

java

double num1, num2;

num1 = Double.parseDouble(numStr1);
num2 = Double.parseDouble(numStr2);

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month