0

I'm getting an error "Choice has not been initialized" on my code but I have already initialized it just before while. The goal is for the file to loop to the first option if the user entered 'y'. I really need your expert help. The code is fine without the do while its just not looping but with it, I'm getting the uninitialized error.

import java.util.Scanner;
/**
 *
 * @author Arren
 */
public class Math {

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        // TODO code application logic here

        int numerator;
        int denominator;
        char Choice;

        String showFractionForm;
        int determineLowestTerm;
        float determineDecimalEquivalent;
        String determineFractionType;
        {
            do {
                Scanner input = new Scanner(System.in);
                System.out.print("Enter the numerator   ==> ");
                numerator = input.nextInt();
                System.out.print("Enter the denominator ==> ");
                denominator = input.nextInt();
                showFractionForm = (numerator + "/" + denominator);
                determineDecimalEquivalent = ((float) numerator) / denominator;
                System.out.println("***************OUTPUT***************");
                System.out.println("NUMERATOR               :  " + numerator);
                System.out.println("DENOMINATOR             :  " + denominator);
                System.out.println("FRACTION                :  " + showFractionForm);
                int smaller = numerator < denominator ? numerator : denominator;
                int HCF = -1;
                for (int i = smaller; i > 0; --i) {
                    if (numerator % i == 0 && denominator % i == 0) {
                        HCF = i;
                        System.out.println("LOWEST TERM             :  " + (numerator / HCF) + "/" + (denominator / HCF));
                        System.out.println("DECIMAL EQUIVALENT      :  " + determineDecimalEquivalent);
                        if (numerator < denominator) {
                            System.out.println("FRACTION TYPE           :  PROPER FRACTION");
                        } else if (numerator > denominator) {
                            System.out.println("FRACTION TYPE           :  IMPROPER FRACTION");
                        } else {
                            System.out.println("FRACTION TYPE           :  WHOLE NUMBER");
                        }
                        System.out.println("");
                        System.out.println("Input again? [y/n] --> ");
                        Choice = input.next().charAt(0);
                    }
                    while (Choice != 'n');
                }
            }
        }
    }
}
2
  • 3
    Indent your code properly, and you will see what the problem is. Your IDE can do that using a single keyboad shortcut. Commented Oct 14, 2019 at 21:09
  • 1
    according to the beautifier I used, the while is not at the correct place... And the init is in an if that may not have been taken Commented Oct 14, 2019 at 21:11

1 Answer 1

1

I just formatted your program well and I am able to run it without any issue. Given below is the formatted program:

import java.util.Scanner;

/**
 *
 * @author Arren
 */
public class Math {

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        // TODO code application logic here

        int numerator;
        int denominator;
        char Choice;

        String showFractionForm;
        int determineLowestTerm;
        float determineDecimalEquivalent;
        String determineFractionType;

        do {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the numerator   ==> ");
            numerator = input.nextInt();
            System.out.print("Enter the denominator ==> ");
            denominator = input.nextInt();
            showFractionForm = (numerator + "/" + denominator);
            determineDecimalEquivalent = ((float) numerator) / denominator;
            System.out.println("***************OUTPUT***************");
            System.out.println("NUMERATOR               :  " + numerator);
            System.out.println("DENOMINATOR             :  " + denominator);
            System.out.println("FRACTION                :  " + showFractionForm);
            int smaller = numerator < denominator ? numerator : denominator;
            int HCF = -1;
            for (int i = smaller; i > 0; --i) {
                if (numerator % i == 0 && denominator % i == 0) {
                    HCF = i;
                    System.out.println("LOWEST TERM             :  " + (numerator / HCF) + "/" + (denominator / HCF));
                    System.out.println("DECIMAL EQUIVALENT      :  " + determineDecimalEquivalent);
                }
                if (numerator < denominator) {
                    System.out.println("FRACTION TYPE           :  PROPER FRACTION");
                } else if (numerator > denominator) {
                    System.out.println("FRACTION TYPE           :  IMPROPER FRACTION");
                } else {
                    System.out.println("FRACTION TYPE           :  WHOLE NUMBER");
                }
            }
            System.out.println("");
            System.out.println("Input again? [y/n] --> ");
            Choice = input.next().charAt(0);
        } while (Choice != 'n');    
    }
}

Sample run:

Enter the numerator   ==> 12
Enter the denominator ==> 4
***************OUTPUT***************
NUMERATOR               :  12
DENOMINATOR             :  4
FRACTION                :  12/4
LOWEST TERM             :  3/1
DECIMAL EQUIVALENT      :  3.0
FRACTION TYPE           :  IMPROPER FRACTION
FRACTION TYPE           :  IMPROPER FRACTION
LOWEST TERM             :  6/2
DECIMAL EQUIVALENT      :  3.0
FRACTION TYPE           :  IMPROPER FRACTION
LOWEST TERM             :  12/4
DECIMAL EQUIVALENT      :  3.0
FRACTION TYPE           :  IMPROPER FRACTION

Input again? [y/n] --> 
y
Enter the numerator   ==> 
Sign up to request clarification or add additional context in comments.

2 Comments

this is not an answer to OP !
Changing bracketing is not just formatting. You should explain what you've done.

Your Answer

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.