Question: I am trying find out if there is a subarray, which has the sum exactly as K. But in some its giving runtime error

![this is some issue ](https://i.stack.imgur.com/7rVDD.png)

```
int sum = 0, count = 0;

HashMap<Integer, Integer> hm = new HashMap<>();
hm.put(0, 1);

for (int i = 0; i < n; i++) {
    sum += arr[i];
    if (hm.containsKey(sum - k)) {
        count = hm.get(sum - k);
        count++;
        hm.put(sum, count);
    }
    else {
        hm.put(sum, 1);
    }
}

if (count > 0) {
    System.out.println("Yes");
}
else {
    System.out.println("No");
}

```

Thank you.

Answer: 
<https://stackoverflow.com/a/288479>

-> It seems that in some old C-Standards you had to declare all variables in the beginning of a method. So I think they are just adhering to this when teaching it like this, but nowadays there is no real reason for this.
Your correct extended version should be

```
Scanner scan = new Scanner(System.in);
String name;
System.out.println("What is your  name?");
name = scan.nextLine(); // no need of String keyword here
System.out.println(name);

```

And why someone would prefer it?

Consider a method definition, May be something like below,

```
public AccountDetails getDetails(Person person){
   Address address;
   AccountInfo accountInfo;
   address = person.getAddresses();
   accountInfo = getDetailsOfAccount();

.......many more lines
   return accountDetails;
}

```

So from the above code, declaring all the variables at the top of the method with proper naming conventions is just helping you out upfront like a sign barrier on the road ahead, that what the method is dealing with.

Other than that, you can follow any version of the code, both are equally same.