Print multiple char variables in one line?

42,675

Solution 1

System.out.println(a+""+b+""+c);

or:

System.out.printf("%c%c%c\n", a, b, c);

Solution 2

You can use one of the String constructors, to build a string from an array of chars.

System.out.println(new String(new char[]{a,b,c}));

Solution 3

The println() method you invoked is one that accepts an int argument.

With variable of type char and a method that accepts int, the chars are widened to ints. They are added up before being returned as an int result.

You need to use the overloaded println() method that accepts a String. To achieve that you need to use String concatenation. Use the + operator with a String and any other type, char in this case.

System.out.println(a + " " + b + " " + c); // or whatever format

Solution 4

This will serve : System.out.println(String.valueOf(a) + String.valueOf(b) + String.valueOf(c));.

Share:
42,675
Kaptain
Author by

Kaptain

Updated on October 04, 2020

Comments

  • Kaptain
    Kaptain over 3 years

    So I was just wondering if there was a way to print out multiple char variables in one line that does not add the Unicode together that a traditional print statement does.

    For example:

    char a ='A'; 
    char b ='B'; 
    char c ='C';
    System.out.println(a+b+c); <--- This spits out an integer of the sum of the characters