简体   繁体   中英

System.out call printing twice

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: Last line of for loop executed twice?

I think it might be a similar sort of oversight.

I also found this, which deals with a similar problem: Java Exception printing twice

but still can't seem to fix mine.

Here is a snippet from one class:

switch (Integer.parseInt(oc, 16)) 
{
  case 0x0F:
  ccrFlagState.zBit(true);
  break;
}

Which calls this snippet in another:

public boolean zBit(boolean set) {

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

When I input 0F the console prints Z set in CCR class twice.

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)

and

if (set == true)

(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 lines?

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:
          zBit(true);
          break;
    }
}


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM