3

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
1
  • You want a String, not to add three chars together. A char is an unsigned 16-bit integer. Commented Oct 3, 2013 at 21:05

6 Answers 6

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

or:

System.out.printf("%c%c%c\n", a, b, c);
Sign up to request clarification or add additional context in comments.

2 Comments

Well that was fast. Seems simple enough. Thanks
I'd use a StringBuilder over the awkward String concatenation (just a preference), but +1 for the printf answer.
3

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}));

Comments

1

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

Comments

1

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

Comments

0
System.out.println(new StringBuilder(a).append(b).append(c).toString());

Comments

-1

System.out.print(a);System.out.print(b);System.out.print(c) //without space

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.