Carlinbhert Toralba
Carlinbhert Toralba

Reputation: 1

Variable ___ might not have been initialized

I just started learning Java for my programming class and I have to create a decimal to binary calculator where it loops until the user inputs "stop", I'm having trouble with needing a different variable type to for the user to input and stop the program.

import java.util.Scanner;

class Main
{
    public static void main(String[] args)
    {
            {

                    //created integer variables for the remainders and an array for binary numbers
                    int remainder, decNumber, i = 1, j;
                    int binaryNumber[] = new int[100];

                    //scanner to take user input
                    Scanner myCalc = new Scanner(System.in);
                    System.out.println("Enter a decimal number: ");
                    String decimalNumber = myCalc.nextLine();


                if (decimalNumber == "STOP")
                    {
                        System. exit(0);
                    } else
                    {
                        decNumber = Integer.parseInt(decimalNumber);
                    }
                
                    remainder = decNumber;

                    //While loop converts the decimal to binary
                    while (remainder != 0)
                    {
                        binaryNumber[i++] = remainder % 2;
                        remainder = remainder / 2;
                    }

                    //Output is shown using the print method
                    System.out.println("This Binary number is ");
                    for (j = i - 1; j > 0; j--)
                    {
                        System.out.print(binaryNumber[j]);
                    }


            }
    }

}

I tried converting the input into an integer if the user types "stop" but it didn't work at all.

Upvotes: 0

Views: 37

Answers (0)

Related Questions