2

Ok so this is my simple java calculator everything works fine I just need help with allowing decimals. Thanks a lot to who ever helps me with this, this is for a school project so I need an answer pretty fast :)

import java.util.Scanner;

public class javaProject

{

    public static void main(String[] args) 
    {
        int x;
        int y;
        String whatOp;      

        Scanner input = new Scanner(System.in);

        System.out.println("please enter the first number");
        x = input.nextInt();

        System.out.println("please enter the second number");
        y = input.nextInt();

        Scanner op = new Scanner(System.in);

        System.out.println("Please enter the operation you want to use");
        whatOp = op.next();

        if (whatOp.equals("+"))
        {
            System.out.println("The answer is " + (x + y));
        }
        else if  (whatOp.equals("-"))
        {
            System.out.println("The answer is " + (x - y));
        }
        else if (whatOp.equals("*"))
        {
            System.out.println("The answer is " + (x * y));
        }
        else if (whatOp.equals("/"))
        {
            System.out.println("The answer is " + (x / y));
        }
        else
        {
           System.out.println("Thats not a choice!");
        }  
    }
}
candied_orange
  • 7,036
  • 2
  • 28
  • 62
TheFalcon
  • 27
  • 1
  • 1
  • 4

6 Answers6

3

Be aware! float and double can break your heart when used with a calculator. These types are not for doing taxes or balancing a checkbook. Why? Because they don't use 10 digit numbers like you and I. They use binary numbers. With whole numbers that's no problem but with fractions you run into problems that are hard to see because java ACCEPT'S them in base 10, DISPLAY'S them in base 10, but it doesn't STORE them in base 10.

That can make it hard to realize that there is no such thing as 0.1 in a float or double. It's as impossible to perfectly express as π is for us in base 10. When you pull it back out it might display that way because of rounding but that is not what was stored and that is not what you'll be calculating against. This is why Calculators do NOT use the IEEE floats and doubles. These types are intended for use with scientific instruments for taking measurements and doing calculations that are fine with precision limits. If you are modeling a calculator like you used in grade school take note that those do NOT use IEEE floats or doubles for this very reason.

For more about this issue read this and this

Instead some very smart people have created something that's perfect for modeling calculators: java.math.BigDecimal;

If you're the visual type let me show you something that might make the difference between BigDecimal and float and double clear:

Inch rulers

These rulers both show inches. For whole inches there is no difference between them. However, they divide inches differently. The top one divides by 10's. The bottom one divides by 2's. The top can be perfectly modeled by BigDecimal. The bottom can be perfectly modeled by float or double. Some of the ticks will line up like .5 because 5/10 = 1/2 but 1/10 = x / (2^n) doesn't have a solution where x and n are whole numbers.

Because humans have a long tradition of using base ten numbers in calculators I don't recommend letting floats or doubles anywhere near your accountant.

import java.math.BigDecimal;
import java.util.Scanner;

public class javaProject
{

    public static void main(String[] args) 
    {
        BigDecimal x; 
        BigDecimal y;
        String whatOp;


        Scanner input = new Scanner(System.in);

        System.out.println("please enter the first number");
        x = input.nextBigDecimal(); 

        System.out.println("please enter the second number");
        y = input.nextBigDecimal(); 

        Scanner op = new Scanner(System.in);

        System.out.println("Please enter the operation you want to use");
        whatOp = op.next();

        if (whatOp.equals("+"))
        {
            System.out.println(  "The answer is " + ( x.add(y) )  );
        }
        else if  (whatOp.equals("-"))
        {
            System.out.println(  "The answer is " + ( x.subtract(y) )  );
        }
        else if (whatOp.equals("*"))
        {
            System.out.println(  "The answer is " + ( x.multiply(y) )  );
        }
        else if (whatOp.equals("/"))
        {
            System.out.println(  "The answer is " + ( x.divide(y) )  );
        }
        else
        {
           System.out.println("Thats not a choice!");
        }
    }
}
candied_orange
  • 7,036
  • 2
  • 28
  • 62
1

I hope you understand the primitive types, such as int, double, etc.

If you want to allow decimals, you're saying that you want doubles. So you just need to make sure that you're accepting doubles, and you're doing math with doubles, as opposed to integer math.

import java.util.Scanner;

public class javaProject

{

public static void main(String[] args) 
{
    double x; //changed both to doubles
    double y;
    String whatOp;


    Scanner input = new Scanner(System.in);

    System.out.println("please enter the first number");
    x = input.nextDouble(); //changed to nextDouble

    System.out.println("please enter the second number");
    y = input.nextDouble(); //same here

    Scanner op = new Scanner(System.in);

    System.out.println("Please enter the operation you want to use");
    whatOp = op.next();

    if (whatOp.equals("+"))
    {
        System.out.println("The answer is " + (x + y));
    }
   else if  (whatOp.equals("-"))
    {
        System.out.println("The answer is " + (x - y));
    }
   else if (whatOp.equals("*"))
   {
       System.out.println("The answer is " + (x * y));
   }
   else if (whatOp.equals("/"))
    {
        System.out.println("The answer is " + (x / y)); //unchanged, you're now doing decimal math!
    }
   else
    {
       System.out.println("Thats not a choice!");
    }


}
}

To remove the trailing .0 you can just remove them easily:

if (s.endsWith(".0"))
    s = s.substring(0,s.length() - 2);

There's also ways to do it with BigNumber or other classes and complex methods, but since this is homework, I think manipulating via string should suffice.

Community
  • 1
  • 1
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
1

Change int x,int y to double x,double y and now you can handle decimal as well.

Pulkit Aggarwal
  • 79
  • 1
  • 10
1

in your program you are using integers. Therefore no decimals can be used. In Java there are multiple types of variables. The one you want to use for decimals is called double. integers and doubles are both primitive data types in java. If you want to learn more about primitive data types just check out this link: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

To make this calculator to be able to have an double type input, then you need to change x and y to doubles instead of an integer:

double x;
double y;

Everything is the same until you want to receive user input, instead of using:

 x = input.nextInt();
 y = input.nextInt();

you need to change the .nextInt(); to a .nextDouble(); because the number you want to be inputted is a double and not a integer. like this:

x = input.nextDouble();
y = input.nextDouble();

I hope this helps.

Mr. Doge
  • 11
  • 1
0

Use double for variables and use nextDouble to capture double values .

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

for your division code, just change the else statement like this:

else if (whatOp.equals("/"))
{
   double tmpX = (new Integer(x)).doubleValue();
   double tmpY = (new Integer(y)).doubleValue(); 
   System.out.println("The answer is " + (tmpX / tmpY));
}
Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • You don't need to change anything else from your original code, and it will properly show divisions with decimals. Or do you also want to do decimal addition, subtraction etc...? – Alvin Bunk Oct 26 '14 at 04:59