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.
-
1Every 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.khelwood– khelwood2020年05月09日 07:45:52 +00:00Commented May 9, 2020 at 7:45
-
1You 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-javaBradley– Bradley2020年05月09日 07:49:23 +00:00Commented May 9, 2020 at 7:49
1 Answer 1
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;