Urgent Java Help!

Discussion in 'Mac Programming' started by lucent, Oct 3, 2007.

  1. lucent macrumors newbie

    Joined:
    Oct 3, 2007
    #1
    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!!!!
     
  2. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    Why not do a search for "algebraic calculator"?
     
  3. Mitthrawnuruodo Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #3
    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?
     
  4. lucent thread starter macrumors newbie

    Joined:
    Oct 3, 2007
    #4
    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
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    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.
     
  6. zechmann macrumors member

    Joined:
    May 8, 2007
    #6
    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
     
  7. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #7
    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.
     
  8. mufflon macrumors 6502

    Joined:
    Sep 15, 2006
    #8
    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
     
  9. lucent thread starter macrumors newbie

    Joined:
    Oct 3, 2007
    #9
    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?
     
  10. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #10
    This is declared, but never used.
     
  11. lucent thread starter macrumors newbie

    Joined:
    Oct 3, 2007
    #11
    when i put in 5.2-3.2=1.99999999996 .... how do i fix this?????
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  13. Jasonbot macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #13
    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;
    }
     
  14. Jasonbot macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #14
    I prefer my solution >_<
     
  15. lucent thread starter macrumors newbie

    Joined:
    Oct 3, 2007
    #15
    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!");
                            }
                    }
            }
                            
                   
    
     
  16. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #16
  17. lucent thread starter macrumors newbie

    Joined:
    Oct 3, 2007
  18. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #18
    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.
     
  19. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #19
    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)
     

Share This Page