2

I am a beginner in Java and Android Studio. I made a (somewhat complex) calculator that calculated fractions along with whole numbers and also mixed fractions. It works by first converting the number entered (whichever format it may be in) into a fraction and then calculating. Here's a part of the code:

buttonAdd.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 String fraction_1 = editTextNum1.getText().toString();
 String fraction_2 = editTextNum2.getText().toString();
 int Num1, Den1, Num2, Den2;
 if (fraction_1.contains("/") && !fraction_1.contains(" ")){
 Num1 = Integer.parseInt(getFraction(fraction_1)[0]);
 Den1 = Integer.parseInt(getFraction(fraction_1)[1]);
 } else if (fraction_1.contains(" ") && fraction_1.contains("/")){
 int[] frac1 = convertFromMixed(fraction_1);
 Num1 = frac1[0];
 Den1 = frac1[1];
 }
 else if (!fraction_1.contains("/")) {
 Num1 = Integer.parseInt(fraction_1);
 Den1 = 1;
 }
 else{
 invalidinput invalidinput = new invalidinput();
 invalidinput.show(getSupportFragmentManager(), "num1error");
 }
 if (fraction_2.contains("/") && !fraction_2.contains(" ")){
 Num2 = Integer.parseInt(getFraction(fraction_2)[0]);
 Den2 = Integer.parseInt(getFraction(fraction_2)[1]);
 } else if (fraction_2.contains(" ") && fraction_2.contains("/")){
 int[] frac2 = convertFromMixed(fraction_1);
 Num2 = frac2[0];
 Den2 = frac2[1];
 }
 else if (!fraction_2.contains("/")){
 Num2 = Integer.parseInt(fraction_2);
 Den2 = 1;
 } else{
 invalidinput invalidinput = new invalidinput();
 invalidinput.show(getSupportFragmentManager(), "num2error");
 }
 int commonDen = getLCM(Den1, Den2);
 Num1 = (commonDen/Den1)*Num1;
 Num2 = (commonDen/Den2)*Num2;
 int addNum = Num1 + Num2;
 if (simplify.isChecked()){
 String result = simplify(addNum, commonDen);
 result = checkFor1(result);
 result = checkForWhole(result);
 textViewResult.setText(result);
 } else {
 String result = addNum + "/" + commonDen;
 result = checkFor1(result);
 textViewResult.setText(result);
 }
 }
 });

I'm getting an error at the last part, where commonDen is calculated. It says that Num1, Num2, Den1 and Den2 have not been initialized. This is just the addition part. I'm getting an error wherever these variables are used for calculation. Please help.

Phantômaxx
38.1k21 gold badges88 silver badges122 bronze badges
asked May 9, 2020 at 7:43
2
  • 1
    Every time you initialise those variables, it's inside an if-statement. You need to specify what values those variables have if none of those initialisations are executed. Commented May 9, 2020 at 7:45
  • 1
    You are initialising your int variables inside if and else if blocks. If the if and else if conditions are never met they never get initialised. Set your variables a default value. Take a look at the following for a helpful guide on initialising variables: dummies.com/programming/java/initialize-variables-java Commented May 9, 2020 at 7:49

1 Answer 1

2

So you have to initialize the variables. You can do it by

changing

int Num1, Den1, Num2, Den2;

to

int Num1 = 0, Den1 = 0, Num2 = 0, Den2 = 0;
answered May 9, 2020 at 7:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.