Variable is not being found

Asked

Viewed 233 times

1

Why the following program does not work?

package javaapplication1;
import java.util.Scanner;

public class Exemplo008 {
    public static void main(String[] args){
        Scanner teclado = new Scanner(System.in);
        int soma =0;
        do{
            System.out.print("Digite um número (0 para sair):");
            int valor = teclado.nextInt();
            soma += valor;
        } while(valor != 0); //Aqui está dando erro.
            System.out.printf("\n A soma dos números digitados é: %d\n",soma);

    }
}

On the line of while, variable cannot be found. This example was used in my college and is copied correctly.

  • 1

    It is a question of scope. Imagine that the scope of the local variable within the method is valid until the "lock keys" of the block in which it was declared. Then the variable valor only goes to the line soma += valor;, because soon after it finds a "lock keys"; as the while of do-while is beyond this key, the variable is out of scope/no longer exists.

  • 1

    Far be it from me to say that you can’t choose the answer you want, but since you switched acceptance sometimes you may not have chosen the one you really want. If it is the one you want, you can leave it where it is, no problem, except for the fact that it is not exactly correct and gives wrong information to those who read here. See [tour]. You can only accept one answer and the one you accept last is the one you will be left with. Want to rethink which one you will accept? When you have 15 points you can vote on all answers to your question or website. You can leave this if you wish.

2 answers

4

You need to declare the variable outside the loop, this is called scope. When you declare the variable inside the block it only exists there; the while is out of the keys, right? Then it is already out of the block and can only access variables declared outside it. So:

package javaapplication1;
import java.util.Scanner;

public class Exemplo008 {
    public static void main(String[] args) {
        Scanner teclado = new Scanner(System.in);
        int soma = 0;
        int valor = 0;
        do {
            System.out.print("Digite um número (0 para sair):");
            valor = teclado.nextInt();
            soma += valor;
        } while (valor != 0);
        System.out.printf("\n A soma dos números digitados é: %d\n", soma);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2


The while is composed of two code blocks:

  • The block of of, contains its own scope of variables that only exist in it.
  • The block while, in turn, it also has its own scope that exists only in it.

The variable value only exists in the scope of of and there’s no way you can access it within the scope of while.

Now... should you declare her out of of the scope of while, as it did with the variable summing up no problem.

  • 1

    There are not two blocks as you say, until there are two of the method and another of the do-while which is internal to the method. the do is not separated from the while it is one thing, a block is defined by keys and not by a command (in some cases the key may be omitted, but there is a block more). The answer is correct, but it is confused by the exact reason.

  • Thanks, I had thought about it, but I thought that the college would not make such a silly mistake and there would be another reason. But apparently, they made it kkkkk

  • @Maniero, allow me to disagree on the code block. There are two blocks of code (the do and the while) and there could be as many as I wish, for example I could do, just below the int sum =0;... {int b = 10;}; and in the next line System.out.println(b); and "b" will never be visible in the println method call();

  • Exactly could exist, but does not exist, Interestingly you explain in the comment how it really is, but in the reply misspelles, your comment contradicts the answer. You know what a block is and answered something else.

  • Ah, yes, @Maniero, true! My Bad!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.