-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)"
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.
– Jéf Bueno
JDK 1.8.0. In case I’m running it in cmd.
– Jow
Dude, this code doesn’t compile . _. the variable
codigo
is defined within the scope ofdo
. There’s no way thewhile(codigo.length() != 12)
work.– igventurelli
There is no difference between "running on CMD" or elsewhere.
– Jéf Bueno
it would not be better to put < 12 instead of != 12, imagine someone’s course to put 13 chars at once...
– h3nr1ke
Exactly, the String
codigo
needs to be declared outside the loop...– Zulian
@LINQ must have altered it and forgot to compile.. it just spun the way it was, I don’t know
– igventurelli
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
– Jow
hehe hit @LINQ :P
– igventurelli
@Jow But the resolution is in Igor’s answer, please mark it as correct.
– Jéf Bueno
@Jow is the check symbol below the number of votes
– igventurelli
@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.
– Jéf Bueno
I marked it as the correct one. I edited the description of the question, which title I put?
– Jow
@Jow I think it’s good like this =)
– igventurelli
@Igorventurelli beauty, thanks.
– Jow
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.
– Victor Stafusa