-1
import java.util.Scanner;

public class InSearchOfAnEasyProblem {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int j;
        for(int i=0; i<n; i++){
            j=sc.nextInt();
            if(j==1){
                System.out.println("HARD");
                break;
            }
        }
        if(j==0)
            System.out.println("EASY");
    }
}

This code doesn't compile, it is showing an error saying variable j might have not been initialized but we are taking input inside the loop even if it's multiple input in the same vairable it should have been just modified right? in that case shouldn't j hold the last input when it comes out of the loop?

2 Answers2

2

Local variables have no default value. As the Java compiler can not tell if the program will ever enter the loop,

int j;
if(j==0)
    System.out.println("EASY");

may be a possible execution path of this code, comparing an uninitialized variable to zero.

Solution: put some value into j, int j=0; would work.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

A local variable has to be initialized. (in your case j)

Had it been a class variable it would automatically be initialized.

Read more here: Variable might not have been initialized error

So, a simple fix would be just to initialize int j = 0;

Ankit Sharma
  • 1,626
  • 1
  • 14
  • 21