Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lucent

macrumors newbie
Original poster
Oct 3, 2007
7
0
I have an assignment due very soon! Any help would be greatly appreciated.

What I need the program to do is act like a calculator. The user will input something like "5.3(space)*(space)4.4" and I need to calculate that. This needs to be a while loop (im guessing) and the (spaces) are suppose to be there. The calculator only needs to be arithmetic operations... if anyone could get me an example it would be greatly appreciated.

Thanks a lot!!!!
 

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
14,665
1,480
Bergen, Norway
So, what have you got, so far...?

Let's see your code, and the error messages showing which part of the code won't compile or maybe even a word or two on where the logical errors seems to appear (if you've gotten that far).

I hope you didn't come here expecting someone else to do your homework for you in its entirety?
 

lucent

macrumors newbie
Original poster
Oct 3, 2007
7
0
Code:
// Simple Calculator
// Author: 
// Last Modified: 10/01/07

import java.io.*;
import java.text.*;
public class Calculator
        {
        public static void main(String[] args)
        throws java.io.IOException
                {
                int i,j;
                String e, e1, e2;
                char s1, s2;
                i=0;

                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);

                System.out.print("What do you want to computer? ");
                e = br.readLine();
                
                while(e[i] != ' ')
                        {   
                        i++;

                        }
                }
        }

can anyone help me finish this up?
I just need to know how to separate it all into variables
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
How do you expect to learn if everyone else does your work? I realise that it can seem difficult but you really need to do the research on your own.

It looks as though someone already gave you a shell to start.
 

zechmann

macrumors member
May 8, 2007
92
0
oh no i really do wanna learn it, i just don't get how to separate all of the variable into 3 so i can compute it... i have looked in my book but I can't seem to find it... I looked under the while loops and found nothing... if someone could just give me a push in the right direction that would be great
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Are the spaces guaranteed? If so , you can tokenize e (poor variable name, but whatever) on the spaces, convert your values to doubles and do a case for your operation.

something like this:

Code:
StringTokenizer st = new StringTokenizer(e, " ");

double leftValue = Double.parseDouble(st.nextToken());
char operator = st.nextToken();
double rightValue = Double.parseDouble(st.nextToken());

if(char == '+')
  return left+right;

else if(char == '-')
  return left-right;

else if(char == '*')
  return left*right;

else if(char == '//')
  return left/right;

else
  return Double.NaN;

That won't compile, and it ain't pretty, but it should get you started.

if the spaces aren't guaranteed, you can scan the line, find the operator, break the line in half at the operator, convert the left and right values, then use the same if/else checks to do the operation.

lots of ways to do it.
 

mufflon

macrumors 6502
Sep 15, 2006
264
2
well you could use something like


Code:
for (i = 0; i < e.length; i++) {
   aChar = e.charAt(i)
   // rest of the code
}
the things you can use are (for this implementation) sumamrized in the Java.Char over @ Sun clickety to Java.Char
 

lucent

macrumors newbie
Original poster
Oct 3, 2007
7
0
Code:
// Simple Calculator
// Author:
// Last Modified: 10/01/07

import java.io.*;
import java.text.*;
import java.util.*;
public class Calculator
        {
        public static void main(String[] args)
        throws java.io.IOException
                {
                int i;
                String equ1;

                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);

                System.out.print("What do you want to computer? ");
                equ1 = br.readLine();
        
                StringTokenizer st = new StringTokenizer (equ1, " ");
                double left = Double.parseDouble(st.nextToken());
                String operation = st.nextToken();
                double right = Double.parseDouble(st.nextToken());
                
                if("+".equals(operation))
                        {
                        System.out.print("The result is: ");
                        System.out.print(left + right + "\n");
                        }
                else if("-".equals(operation))
                        {
                        System.out.print("The result is: ");
                        System.out.print(left - right + "\n");
                        }
                else if("*".equals(operation))
                        {
                        System.out.print("The result is: ");
                        System.out.print(left * right + "\n");
                        }
                else if("/".equals(operation))
                        {
                        System.out.print("The result is: ");
                        System.out.print(left / right +"\n");
                        }
                else
                        {
                        System.out.print("You did not enter an arithmetic ope$
                        }
                }
        }

suprisingly I did it most on my own... feels good

any suggestions on it?
 

lucent

macrumors newbie
Original poster
Oct 3, 2007
7
0
when i put in 5.2-3.2=1.99999999996 .... how do i fix this?????
 

Jasonbot

macrumors 68020
Aug 15, 2006
2,467
0
The Rainbow Nation RSA
lucent said:
when i put in 5.2-3.2=1.99999999996 .... how do i fix this?????

Java alwasy does silly things like that. you could always convert the ans. to a string and .substring it to remove teh decimals.

or try this to round up (hope you understand my crappy example)

whatever=finalAns+"";
int dec= whatever.charAt('.');

string subans = whatever.substrin(dec,dec+1);

int subansInt =Integer.parseInt(subans);

if(subans>5) {
finalAns=finalAns+1;
}
 

lucent

macrumors newbie
Original poster
Oct 3, 2007
7
0
i tried using big decimals but it wouldn't work :(... what am i doing wrong?

Code:
import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;
public class Calculator
        {
        public static void main(String[] args)
        throws java.io.IOException
                {
                String equ1;

                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);

                System.out.print("What do you want to computer? ");
                equ1 = br.readLine();

                StringTokenizer st = new StringTokenizer (equ1, " ");
                double left = Double.parseDouble(st.nextToken());
                String operation = st.nextToken();
                double right = Double.parseDouble(st.nextToken());

                if("+".equals(operation))
                        {
                        BigDecimal final1 = new BigDecimal(left + right);
                        System.out.print("The result is: ");
                        System.out.print(final1 + "\n");
                        }
                else if("-".equals(operation))
                        {
                        BigDecimal final2 = new BigDecimal(left - right);
                        System.out.print("The result is: ");
                        System.out.print(final2 + "\n");
                        }
                else if("*".equals(operation))
                        {
                        BigDecimal final3 = new BigDecimal(left * right);
                        System.out.print("The result is: ");
                        System.out.print(final3 + "\n");
                        }
                else if("/".equals(operation))
                        {
                        BigDecimal final4 = new BigDecimal (left / right);
                        System.out.print("The result is: ");
                        System.out.print(final4 +"\n");
                        }
                else
                        {
                        System.out.print("You did not enter an arithmetic operator!");
                        }
                }
        }
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
To solve this you need to use formatting to print the number to only a certain number of decimal places, e.g. %5.2f prints xx.xx where x is a digit.

I haven't done Java for a while so I can't remember exactly how it works.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
I just built your code and did 5.2-3.2 ... i got a 2.

i built your old code and got 2.0, as well.


Microsoft Windows XP [Version 5.1.2600]
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.