Variable not seemed to be initialized

import java.util.Scanner;

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

    int antallTall;
    int antallLest;
    double nesteTall;
    double minsteVerdi;
    double nestMinsteVerdi;

    Scanner tast = new Scanner(System.in);

    System.out.println("Hvor mange tall?");
    antallTall = tast.nextInt();
    tast.nextLine();

    antallLest= 1;

    System.out.println("Skriv første tall: ");
    minsteVerdi = tast.nextDouble();
    tast.nextLine();

    for(antallLest=1;antallTall>antallLest;antallLest++){
      System.out.printf("Skriv %d. tall: ",antallLest+1);
      nesteTall = tast.nextDouble();
      tast.nextLine();

      if(nesteTall>minsteVerdi)
        nestMinsteVerdi=nesteTall;

      if(nesteTall<minsteVerdi)
        nesteTall = minsteVerdi;

      if(nesteTall>minsteVerdi && nesteTall<nestMinsteVerdi) 
      nestMinsteVerdi = nesteTall;   

    }

    System.out.printf("Minste verdien av tallene du har oppgitt er: %f\n",minsteVerdi);
    System.out.printf("Nest minste verdi av tallene du har oppgitt er: %f\n",nestMinsteVerdi);
    tast.close(); 
  }
}  

It's a program supposed to calculate the lowest and second lowest number that the user provides.

For some reason, it says that the local variable nestMinsteVerdi is not initialized, and I can't seem to figure out why or where the fault is. I have been struggling with this for a good 2 hours.

Thanks in advance.

Upvotes: 0

Views: 52

Answers (2)

Andy Thomas
Andy Thomas

Reputation: 86381

In Java, a local variable needs to have a definitely assigned value before its value is accessed.

You declare your local variable without assigning it a value. This is okay in Java, because the compiler will ensure that you give it a value before using it.

double nestMinsteVerdi;

Then set it if a condition is true:

if(nesteTall>minsteVerdi)
  nestMinsteVerdi=nesteTall; 

Then access it in the conditional of an if statement. But if the condition above were false, the value of nestMinisteVerdi would not yet be assigned.

if(nesteTall>minsteVerdi && nesteTall<nestMinsteVerdi) 
   nestMinsteVerdi = nesteTall;  

Since there's at least one way for the code to execute in which no value is assigned, the compiler complains. This is a good thing. Accidentally uninitialized variables can be a frequent source of defects.

From the Java Language Specification, Chapter 16: Definite Assignment:

The idea behind definite assignment is that an assignment to the local variable or blank final field must occur on every possible execution path to the access.

Upvotes: 3

Daniel
Daniel

Reputation: 169

Andy Thomas's answer is correct. You do not assign a value to the variable.

double nestMinsteVerdi;

The compiler will not allow you to run because the only assignment is within a conditional that could conceivably fail, and if it were to fail, the variable would not be assigned a value. Since code can't be compared to a variable with no value, this would break execution, so the compiler will not allow it.

Upvotes: 0

Related Questions