Llamada system.out imprimiendo dos veces

I put off asking this as it must be something daft and, given it was also me who asked this, I hesitated and have spent the last few hours getting increasingly frustrated: ¿Última línea del bucle for ejecutado dos veces?

I think it might be a similar sort of oversight.

I also found this, which deals with a similar problem: Excepción de Java imprimiendo dos veces

but still can't seem to fix mine.

Here is a snippet from one class:

switch (Integer.parseInt(oc, 16)) 
  case 0x0F:

Which calls this snippet in another:

public boolean zBit(boolean set) {

    if (set = true) {
        System.out.println("Z set in CCR class");
    return set;

Cuando entro 0F the console prints Z set in CCR class dos veces.

Can anyone point me in the right direction as to why? Thanks, Robert.

-EDIT- I should add that I get the same result with:

if (set)


if (set == true)

preguntado el 29 de junio de 12 a las 20:06

@thinksteep It does return true, in fact, it always does. When you assign a value to a variable the output is the value you assigned. The problem is, I think, it shouldn't be an assignment? -

doble = para comparacion: if (set == true) o simplemente if (set). Don't know why this is causing 2 prints from the code shown -

I'm on Java6 and yes, it seems to compile and run fine. -

@TheZ: you are correct, I have removed my comment -

The problem is not covered by the provided code snippets. Please provide more code that really makes this problem reproducable for us. -

2 Respuestas

This is not reproducable for me. This code only pints it out once. Please provide more code ...

public static void main(String[] args) {
    String oc = "0F";

    switch (Integer.parseInt(oc, 16)) {
      case 0x0F:

public static boolean zBit(boolean set) {
    if (set) {
        System.out.println("Z set in CCR class");
    return set;

Respondido el 29 de junio de 12 a las 20:06

Have to write this as an answer, because it' too much for a comment. - Fabián Barney

Hi Fabian, There is another piece of code in the GUI class and it does indeed jump past it, printing "Z is set".: if (ccrFlags.zBit(false)) { Z.setText(unset); } else { Z.setText(flagSet); System.out.println("Z is set"); } I guess there's something wrong with the first part of the "if" statement? - Robert

(Your condition needs to be set == true. set = true assigns the variable to true and then always evaluates to true.)

How sure are you that you are not calling zBit twice? Use a debugger to know for sure.

You don't have some kind of logging wrapper or system possibly intercepting and repeating System.out ¿líneas?

Respondido el 29 de junio de 12 a las 20:06

Or even more succintly: if ( set ). It's much more readable. - bizcocho

Completely agree, was just trying to highlight the nature of the error. == true siempre es redundante. - Sean Owen

@Sean: I think it is calling zBit twice right enough. Not sure why yet but I'm trying to figure it out with the debugger. Many thanks for your help. - Robert

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.