0

I already initialized the variable yn, but it keeps saying that I didn't. Tried to initialized it directly on the scanner but it has error and say again that it's already been initialized in the method.

import java.util.Scanner;

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

    Scanner s = new Scanner(System.in);

    int num;
    char yn;

    while (yn == 'Y' && yn == 'y')
    {
      do
      {
        System.out.print("Enter a positive integer: ");
        num = s.nextInt();
        if (num < 0)
        {
          System.out.print(num + "is a negative integer. Please try again!");
        }
        else
        {
          if (num % 2 == 0)
          {
            System.out.println(num + " is an even number.");
          }
          else
          {
            System.out.println(num + " is an odd number.");
          }
          continue;
        }
      }
      while (num < 0);
      System.out.println("Press Y if you want to input again and N if no.");
      yn = s.next().charAt(0);
      do
      {
        if (yn == 'N' && yn == 'n')
        {
          System.out.println("Done!");
          break;
        }
        else if (yn == 'Y' && yn == 'y')
        {
          System.out.println("Done!");
          continue;
        }
        else
        {
          System.out.println("Invalid Input! Try again!");
          break;
        }
      }
      while (yn != 'Y' && yn != 'y' && yn != 'N' && yn != 'n');
    }

  }
}
Sandeepa
  • 3,457
  • 5
  • 25
  • 41

4 Answers4

3

You declare yn without an initial value and then immediately use it as loop condition.

char yn;

while(yn == 'Y' && yn == 'y'){

would imply you expect a default value of y (or Y). You must explicitly set it so for that to be true. Also, no character is both 'Y' and 'y' so you need a logical or. Like,

char yn = 'Y';

while(yn == 'Y' || yn == 'y') {

You might also consider

char yn = 'Y';
while (Character.toUpperCase(yn) == 'Y') {

and then you don't need an ||. You have the same logical impossibility here

if(yn == 'N' && yn == 'n'){
    System.out.println("Done!");
    break;
}
else if(yn == 'Y' && yn == 'y'){
    System.out.println("Done!");
    continue;
}

and can fix it with || (or with Character.toUpperCase(char)).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Wow. Thank you! Now I learned more, and fully understand how while works. Thanks! Will take note all of this next time. – Joshua Ungson Oct 06 '19 at 11:01
1
char yn;

This is where you declare the variable.

while(yn == 'Y' && yn == 'y'){

This is where you use the variable. Nowhere in between these two lines is yn initialized - that's why it's used uninitialized.

Sören
  • 1,803
  • 2
  • 16
  • 23
0

Think of "Yn" as a container, you need to have something in the container if you want to test what is inside that container, or in this case

char yn;

Should become

char yn = 'Y';

You test with your while loop and your if statements

char yn; //You are DECLARING a variable
char yn = 'Y' //You are Initialising a variable

In your case, you can say

char yn = ' '; 

Because I believe that is what you tried to do

I also noticed you are trying to test if your yn variable is a capital and lowercase at the same time, you can fix this by changing the && (AND) to || (OR)

jaco
  • 43
  • 11
  • 1
    Now I understand. I need to initialize the yn to Y which is while's condition, for the statement inside the while to start. Thanks! – Joshua Ungson Oct 06 '19 at 11:04
0

In Java whenever you declare a variable but do not initialize it (like in your case char yn;), the compiler checks for its initialization part further.

If you later initialize it inside any scope which has a condition (like in your case while (yn == 'Y' && yn == 'y'){..}), then compiler is not able to decide at compile time that whether the code inside the scope or block will get executed or not.

Hence there is no assurance to compiler although you have initialized inside the block.

Therefore compiler throws an error stating, variable not initialized.

Adarsh Kumar
  • 467
  • 6
  • 11