Home > Mobile >  Stop logger to print from new line
Stop logger to print from new line

Time:06-22

I'm new to java, trying to use logger instead of System.out.print();

Making cows and bulls game and can't figure out how to make logger printing each digit from new line and instead print it as one number.

Example :

logger.info(player   ", you lose. The number is : ");
                    for (int i = 0; i < random.length; i  ) {
                        logger.info("{}", random[i]);
                    }
                }

output is :

17:54:27.668 [main] INFO com.bullsandcow.GameScreen - Viktors, you lose. The number is : 
17:54:27.668 [main] INFO com.bullsandcow.GameScreen - 3
17:54:27.673 [main] INFO com.bullsandcow.GameScreen - 6
17:54:27.673 [main] INFO com.bullsandcow.GameScreen - 4
17:54:27.673 [main] INFO com.bullsandcow.GameScreen - 7

What I want is to print 3647 as one number.

CodePudding user response:

Assuming random is int[], what you need is to combine all element into a String.

        ...
        StringBuilder builder = new StringBuilder();
        for(int i: random){
            builder.append(i);
        }
        logger.info("{}", builder.toString());

CodePudding user response:

i'm not sure what you want, but if you just need the random number in the same line, you can try:

String s = "";
for (int i = 0; i < random.length; i  ) {
    s  = random[i];
}
logger.info("{}", s);
  • Related