0

I am new to java programming language. I've been practicing the while loop statement with Strings and I can't seem to get this code right. I've been getting error:

variable choose might not have been initialized

import java.util.Scanner;

public class Test {
 public static void main(String args []) {
 Scanner sc=new Scanner(System.in);

 int a, b,c, diff, prod, q, choice;
 String name;
 String choose;


 System.out.print("Enter Name Please:       \t");
 name = sc.next();

 System.out.println("WELCOME  " + name);
 System.out.println("");

while (choose == "Y");
{
 System.out.println("1. Addition  \t2. Subtraction \t3. Multiplication \t4. Division");
 System.out.println("Please Choose a number: \t");
 choice = sc.nextInt();

 switch (choice)
 {
   case 1:
       System.out.println("Thank You "+name+", you have chosen ADDITION");
       System.out.print("\nPlease Enter the first number: \t");
       a = sc.nextInt();
       System.out.print("\nPlease Enter the Second number: \t");
       b = sc.nextInt();

       c = a + b;

       System.out.println("\nYou have chosen "+a+ " as your first number, we will add it to "+b+" your second number.");
       System.out.println("\nThe answer is "+c);

       System.out.println("\nWould you like to choose again? Y/N: \t");
       choose = sc.next();

       break;
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76

6 Answers6

2

You use the variable choose before you set it to an initial value. You don't ever assign choose any value before you use it in the while loop.

Add this statement before the while loop:

choose = sc.next();

Of course, you'll want an appropriate message for the end user.

Also, your comparison is incorrect - use choose.equals("Y") or choose.equalsIgnoreCase("Y"), since you don't compare objects with ==.

[EDIT]: Looking back at your code, your while loop would be busted anyway. Remove the semicolon from the end of it. This way, it will enter the loop when you expect it to.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
1

This is a safety net built into the compiler and it's complaining that String choose may not have been declared before it is used in your while statement. Declare it to "" and you'll be good to go.

String choose = "";

Hope this helps! Welcome to Java!

dsrees
  • 6,116
  • 2
  • 26
  • 26
0

You did not assign any value to variable choose.

Alex
  • 11,451
  • 6
  • 37
  • 52
0

change String choose; to String choose = ""; this will initialize the String choose.

Sean F
  • 2,352
  • 3
  • 28
  • 40
0

The variable choose is not being initialized to a value, and is just being declared. Maybe you want String choose = "Y";? Maybe you wanted another user input with Scanner after entering the name?

AbhishekJoshi
  • 63
  • 2
  • 8
0

You need to initialize choose ,that is, assign a value to it while declaring it. Maybe you could do this - String choose = "Y". And as others have mentioned, using equalsIgnoreCase("Y") would be better.

Utsav T
  • 1,515
  • 2
  • 24
  • 42