Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: no-tard, mid urgent, Help request: Calculating rational problems (making a calculator)

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question no-tard, mid urgent, Help request: Calculating rational problems (making a calculator)

    Hello there pepz and potential future colleagues,

    I'm facing a problem concerning one particular school assignment called "Rational calculator". Basically what it does is, it returns result of inserted rational expression in exact value, which can be either integer or fraction. The thing is, I got no idea how to add particular operator inside the program that could easily add, subtract or multiply numbers inserted, that would be within brackets for example.

    Thanks


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: no-tard, mid urgent, Help request: Calculating rational problems (making a calculator)

    You could make the user choose through some sort of menu before typing in numbers, however, this would be rather irritating and clunky.

    What you would want to do is to check for a '+', '-' or a '*' sign in the string that the user inputs, then divide the part of the string that is before that letter, and the part that's after and then convert them into ints. After that you simply calculate them using the sign that was in the equation.

    For example, something like this:

     
    String equation = "2*1132";
    int part1, part2;
     
            if(equation.indexOf('*') != -1){ //Checks if the string contains '*', else the method returns -1
     
            /*Note, I don't know if this string to int conversion works, however there are other easy ways to do it otherwise*/
     
              part1 = (int) equation.substring(0, equation.indexOf('*')-1); 
             //(Above) Creates a substring that begins at the start of the equation string and ends before the math sign
     
             part2 = (int) equation.substring(equation.indexOf('*')+1, equation.length()); 
             //(Above) Creates a substring that begins at 1 character after the math-sign and ends at the end of the equation-string
     
             System.out.println(part1 + " * " + part2 + " = " + part1*part2); //Prints out the equation and answer
     
            }//End if
     
           //=====================================================//
          //=====================================================//
     
         else if(equation.indexOf('+') != -1){ //Checks if the string contains '+', else the method returns -1
     
             part1 = (int) equation.substring(0, equation.indexOf('+')-1); 
     
             part2 = (int) equation.substring(equation.indexOf('+')+1, equation.length()); 
     
             System.out.println(part1 + " + " + part2 + " = " + part1+part2); //Prints out the equation and answer
     
            }//End else-if
     
           //=====================================================//
          //=====================================================//
     
          else if(equation.indexOf('-') != -1){ //Checks if the string contains '-', else the method returns -1
     
     
             part1 = (int) equation.substring(0, equation.indexOf('-')-1);
     
             part2 = (int) equation.substring(equation.indexOf('-')+1, equation.length()); 
     
             System.out.println(part1 + " - " + part2 + " = " + part1-part2); //Prints out the equation and answer
     
            }//End else-if
     
           //=====================================================//
          //=====================================================//
     
     
         else{
     
                System.out.println("You entered something invalid, stupid. I mean " + equation + " isn't even a proper equation!");
     
     
        }//End else

    Should resort in the first "if" and part1 becoming '2', part2 "1132".
    In this case the printed line would be "2 * 1132 = 2264".

    Note that this only works with one sign in the equation, so for example "1*5+2" wouldn't work with this way of coding.

    Also note that if this is for some java class, and your teacher writes an equation wrong (like 69 instead of 6+9), it would result in "your" program telling him/her that he/she is stupid ("You entered something invalid, stupid. I mean 69 isn't even a proper equation").

    --- Update ---

    Just realized, you wrote "pepz" and then I came
    Last edited by pepzi999; November 21st, 2012 at 03:25 AM. Reason: Spelling errors

Similar Threads

  1. I have tried making a simple random calculator and its not working?
    By StackOfCookies in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 04:00 PM
  2. Problems Making a "POST" to a Socket
    By haroldjclements in forum Java Networking
    Replies: 0
    Last Post: October 11th, 2011, 05:28 AM
  3. JButtons making a Phone Calculator
    By falkowa42 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 10th, 2011, 07:03 PM
  4. So im making a freehand Line,Rect,Oval; but problems
    By Matta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2011, 07:25 AM
  5. Making a pacman game having some problems with drawing
    By Matta in forum What's Wrong With My Code?
    Replies: 22
    Last Post: June 9th, 2011, 06:18 PM

Tags for this Thread