-1

I am trying to make a calculator between fractions and that if the user inputs a +,-,*, or / it will correspond in the case. This is the code I have so far:

import java.util.Scanner;
public class calculator 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String x,y;


        System.out.println("Enter first fraction in a / b form: ");
        x = input.nextLine();

        System.out.println("Enter operation: ");
        char z = input.next().charAt(0);

        System.out.println("Enter second fraction in c / d form: ");
        y = input.nextLine();

        String aString = x.substring(0,1);
        String bString = x.substring(4,5);
        String cString = x.substring(0,1);
        String dString = x.substring(4,5);

        int a = Integer.parseInt(aString);
        int b = Integer.parseInt(bString);
        int c = Integer.parseInt(cString);
        int d = Integer.parseInt(dString);


        int answer = 0;
        switch (z)
        {
            case '+':
                answer = (a/b) + (c/d);
                break; 
            case '-':
                answer = (a/b) - (c/d);
                break;
            case '*':
                answer = (a/b) * (c/d); 
                break;
            case '/':
                answer = (a/b) /(c/d);
                break;
            default:
                System.out.println("ERROR");
                break;              
        }
        System.out.println("Answer = " + answer);       
    }
}

OUTPUT SHOULD BE

Enter first fraction in a / b form: 
1 / 2
Enter operation: 
+
Enter second fraction in c / d form: 
2 / 5
answer = 9/10
4
  • 2
    What is the problem you're experiencing? Wrong output? Error message? Please share.
    – rgettman
    Feb 24, 2014 at 19:06
  • 2
    In particular, there's no sign of an actual question here...
    – Jon Skeet
    Feb 24, 2014 at 19:10
  • Your switch is fine, @AJ. has pointed a logical error, but you have a much greater problem which is that to output a fraction you need to do fraction arithmetic. You can do the computation with double so you don't end up with 0 but that will get you a decimal number.
    – Radiodef
    Feb 24, 2014 at 19:20
  • i made the changes @AJ has said but im getting this as i input '+': Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.substring(String.java:1907) at calculator.main(calculator.java:33) Feb 24, 2014 at 19:35

2 Answers 2

1

The problem is here

 String aString = x.substring(0,1);
 String bString = x.substring(4,5);
 String cString = x.substring(0,1);
 String dString = x.substring(4,5);

a=c and b=d

These two line

 String cString = x.substring(0,1);
 String dString = x.substring(4,5);

should be

String cString = y.substring(0,1);
 String dString = y.substring(4,5);

Before switch print the values of a b c d z then you will come to know about your correctness.

One more thing is, you are doing division of int variables so the result will be in int only.

I suggest you to change the types of a b c d and answer to double and use Double.parseDouble() for converting string to double.

8
  • That's true but the OP's code has more than one problem. Such as that their output shows they expect a fraction but answer is computed as an int.
    – Radiodef
    Feb 24, 2014 at 19:13
  • @Radiodef Yes, thats correct. Let me include this in answer.
    – AJ.
    Feb 24, 2014 at 19:14
  • thank you!! i made the changes @AJ has said but im getting this as i input '+': Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.substring(String.java:1907) at calculator.main(calculator.java:33) Feb 24, 2014 at 19:37
  • @user3348064 Which line is at 33.
    – AJ.
    Feb 24, 2014 at 19:40
  • String cString = y.substring(0,1); Feb 24, 2014 at 19:42
0

This problem is so suited for OOP, an extra class:

public class Q {

    public final int numerator;
    public final int denominator;

    public static Q valueOf(String representation) {
        Pattern pattern = Pattern.compile("([-]?\\d+) *[/:] *([-]?\\d+)");
        Matcher matcher = pattern.matcher(representation.trim());
        if (!matcher.matches()) {
            throw new IllegalArgumentException(
                "Not a quotient (like '3/4'): " + representation);
        }
        int num = Integer.parseInt(matcher.group(1));
        int den = Integer.parseInt(matcher.group(2));
        return new Q(num, den);
    }

    public Q(int num, int den) {
        if (den < 0) {
            den = -den;
            num = -num;
        }
        int c = gcd(Math.abs(den), Math.abs(num));
        denominator = den / c;
        numerator = num / c;
    }

    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }

    public Q add(Q rhs) {
        int c = gcd(denominator, rhs.denominator);
        int den = (denominator / c) * rhs.denominator;
        int num = numerator * (rhs.denominator / c)
                + rhs.numerator * (denominator / c);
        return new Q(num, den);
    }

    public Q mult(Q rhs) {
        int den = denominator * rhs.denominator;
        int num = numerator * rhs.numerator;
        return new Q(num, den);
    }

    public static int gcd(int x, int y) {
        assert x >= 0 && y >= 0;
        while (x != y) {
            if (x > y) {
                x -= y;
            } else {
                y -= x;
            }
        }
        return x;
    }
}

Using this class several small problematic code parts disappear.

Not the answer you're looking for? Browse other questions tagged or ask your own question.