Loop code not satisfied

Asked

Viewed 52 times

-1

import java.util.Scanner;

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

        do {            

            Scanner sc = new Scanner(System.in);
            System.out.println("Informe os primeiros 12 caracteres do codigo de barras: \n");
            String codigo = sc.nextLine();

        } while (codigo.length() != 12); 
    }
}

I am trying to make you repeat the message and read the user’s data until he enters a string with exactly 12 characters, but is giving following error: "variable code is already defined in method main(String[] args)"

  • 2

    Which JDK (and JDK version) are you using? For me this code does not compile the way it is, and when I arrange to compile it works normally.

  • JDK 1.8.0. In case I’m running it in cmd.

  • 2

    Dude, this code doesn’t compile . _. the variable codigo is defined within the scope of do. There’s no way the while(codigo.length() != 12) work.

  • 1

    There is no difference between "running on CMD" or elsewhere.

  • 1

    it would not be better to put < 12 instead of != 12, imagine someone’s course to put 13 chars at once...

  • 1

    Exactly, the String codigoneeds to be declared outside the loop...

  • @LINQ must have altered it and forgot to compile.. it just spun the way it was, I don’t know

  • I don’t know what I did, I think I drove without saving, because I wasn’t complicated msm. I declared the string out of the loop, and I took the scanner class out tbm. Agr worked! About the CMD thing, I’m starting agr, it was bad for noobada kk Thanks guys, it worked :D

  • hehe hit @LINQ :P

  • @Jow But the resolution is in Igor’s answer, please mark it as correct.

  • @Jow is the check symbol below the number of votes

  • 1

    @Igorventurelli Yes, I left my vote =D I was finding it strange, but you know how it is, I kept a mistrust of the version.

  • 1

    I marked it as the correct one. I edited the description of the question, which title I put?

  • @Jow I think it’s good like this =)

  • @Igorventurelli beauty, thanks.

  • Just to be clear, this question had 3 votes to close as not clear enough and 2 as typo (including mine), but Stackoverflow only shows that reason that had most of the closing votes.

Show 11 more comments

1 answer

3


The problem is in the scope of the variable codigo. In fact, this code does not compile.

The variable codigo exists only in the scope of do. Thus, the while(codigo.length() != 12) won’t work.

Declare it out and the problem will be solved:

import java.util.Scanner;

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

        String codigo = "";
        do {            

            Scanner sc = new Scanner(System.in);
            System.out.println("Informe os primeiros 12 caracteres do codigo de barras: \n");
            codigo = sc.nextLine();

        } while (codigo.length() != 12); 
    }
}
  • I did a little different on the fix. I threw the Scanner class out of the loop and declared the variable "code" out of the loop as well. Would there be some problem, or some difference besides the variable and class stay in the local/global scope?

  • @Jow putting himself in the "global scope" (this concept does not exist in Java. We call it the attribute/field) needs to make them static, otherwise they cannot be used within the current method (which is static). It is better to put the line instance the Scanner out of the loop. So you create only one instance and do not fill the memory :)

  • got it. mto thanks

Browser other questions tagged

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