Home > Enterprise >  variable sumEven might not have been initialized
variable sumEven might not have been initialized

Time:04-13

The program is from array chapter in Java for class 10

QWrite a program to store 4 values from the user and print the sun of elements at even indices

import java.util.*;
public class indices {
  public static void main(String args[]) {
    int sumEven;
    int even[]= new int[4];
    Scanner sc = new Scanner (System.in);
    System.out.println("enter 4 numbers");
    for (int i=0;i<4;i  )
      even[i]= sc.nextInt();

    for(int i=0;i<4;i  )
      if(even[i]%2==0)
        sumEven = (sumEven   even[i]);

    System.out.println("sum of even indices are:"  sumEven);
  }
}

The error it shows is

even_indices.java:18: error: variable sumEven might not have been initialized
sumEven = (sumEven   even[i]);
             ^

even_indices.java:19: error: variable sumEven might not have been initialized
System.out.println("sum of even indices are:"  sumEven);
                                               ^
2 errors

Whats the issue? I did initialize it, no?

CodePudding user response:

Explanation

Java requires your variable to have a value in all paths that could lead to its usage-site. In your case, the critical call-site is:

System.out.println("sum of even indices are:"  sumEven);

But your code has a path leading to this line of code, where you never executed sumEven = ..., i.e. a path that totally skips your:

sumEven = (sumEven   even[i]);

leading to a usage of sumEven without it having a value. That is invalid, hence you get an error message.

The problematic path is when your array has no even values. Suppose the user only enters odd values, such as:

enter 4 numbers
3 7 1 5

In this case, the if, which guards the assignment to sumEven, is never entered and hence you never execute sumEven = ....

path


Solution

You need to ensure that sumEven has a value, in all paths. So you have to decide what value it should have if all values are odd.

A reasonable choice might be 0. So you could just start the variable directly with the value 0 when you originally create it:

int sumEven = 0;

and Java will be happy.


Notes

Your code has a couple of issues that you should address to minimize further bugs:

  1. Do not leave out curly braces, even if it is a one-liner.

Statements like

for(int i=0;i<4;i  )
  if(even[i]%2==0)
    sumEven = (sumEven   even[i]);

are quite dangerous and can easily lead to bugs which are very hard to trace back (if you are a beginner). You can avoid them all together by just never ommiting the curly braces:

for(int i=0;i<4;i  ) {
  if(even[i]%2==0) {
    sumEven = (sumEven   even[i]);
  }
}
  1. Do not use C-style array syntax

You wrote

int even[]= new int[4];

But this is a legacy syntax which is faded out. It is not supported by Java on all features and will not be supported on new features anymore. Instead, write it the Java-way:

int[] even = new int[4];
  1. Follow naming conventions

You wrote:

public class indices {

But class names are supposed to be written in PascalCase. So instead write:

public class Indices {

Putting everything together

import java.util.Scanner;

public class Indices {
  public static void main(String[] args) {
    int sumEven = 0;
    int[] values = new int[4];
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter 4 numbers:");
    for (int i = 0; i < 4; i  ) {
      values[i] = scanner.nextInt();
    }

    for (int i = 0; i < 4; i  ) {
      if (values[i] % 2 == 0) {
        sumEven = sumEven   values[i];
      }
    }

    System.out.println("Sum of even values are:"   sumEven);
  }
}

You might even spot more improvements in this code version, compared to yours :)

  • Related