0

I am working on a project for my computer science class. I am making a math tool that can do things like calculate fractions, find areas and perimeters, etc. I have been making functions for my fraction calculator and the area but I keep getting an error that says "float outputDecimal might not have been initialized.

import java.util.Scanner; 
import static java.lang.System.*;
import javax.swing.*;
import javax.swing.JOptionPane;

public class fractionCalculator
{
    static float fracCalc(float num1, float den1, float num2, float den2, char operator)
    {
        float outputDecimal;
        if (operator == 'a') //for fraction addition
        {
            outputDecimal = (num1/den1) + (num2/den2);
        }
        if (operator == 's') //for fraction subtraction
        {
            outputDecimal = (num1/den1) - (num2/den2);
        }
        if (operator == 'd') //for fraction division
        {
            outputDecimal = (num1/den1) / (num2/den2);
        }
        if (operator == 'm') //for fraction multiplication
        {
            outputDecimal = (num1/den1) * (num2/den2);
        }
        return outputDecimal;
    }

     public static void main(String[] args)...
  ...System.out.printf("\nType in the first numerator: ");
     nume1 = read.nextFloat(); //reads the first numerator
     read.nextLine();
     System.out.printf("\ntype in the first denominator: ");
     dene1 = read.nextFloat(); //reads the first denominator
     read.nextLine();
     System.out.printf("\ntype in the second numerator: ");
     nume2 = read.nextFloat(); //reads the second numerator
     read.nextLine(); 
     System.out.printf("\ntype in the second denominator: ");
     dene2 = read.nextFloat(); //reads the second denominator
     read.nextLine();
     finalDecimal = fracCalc(nume1, dene1, nume2, dene2, fracMode);
     System.out.printf(" = " + finalDecimal);
}

does anybody know why the initialization inside the if statements in my fracCalc function is not working?

0 Answers0