1

I have a problem with unicodes in java. I am programming in eclipse and need to edit "Hello World!" followed by a heart and a penguin. Everything works fine besides the penguin. Somehow I can not edit an unicode with more than 4 characters. This is my code:

package HelloWorld;

public class HelloWorld extends MiniJava{

public static void main(String[] args) {
    String s1 = "Hello World! ";
    char c1 = '\u2661';
    char c2 = ''; //\u1F427
    write (s1+c1+c2);

}

}

Can you help me with that?

Z.Wo
  • 101
  • 2
  • 10
  • Why you can't? What's the error you're getting? – Maroun Oct 29 '17 at 11:54
  • 2
    https://stackoverflow.com/a/24633876/6253321 – cpp beginner Oct 29 '17 at 11:55
  • The error I am getting is following: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Invalid character constant at HelloWorld.HelloWorld.main(HelloWorld.java:8) – Z.Wo Oct 29 '17 at 12:03
  • 1
    Try making `c2` into a String with value of `"\uD83D\uDC27"` (thanks to the link from @cppbeginner comment). – Vasan Oct 29 '17 at 12:10
  • When I am making c2 as a String like you purposed, there appears joust "Hello World!" <3 but no penguin at all and there is no error from java, why that? – Z.Wo Oct 29 '17 at 16:27
  • public static void main(String[] args) { String s1 = "Hello World! "; char c1 = '\u2661'; String s2 = "\uD83D\uDC27"; write (s1+c1+s2); – Z.Wo Oct 29 '17 at 16:48
  • `String c1 = "♡"; String c2=""; ` should also work. – Tom Blodget Oct 29 '17 at 17:29

1 Answers1

2

As already mentioned in the comments by linking to another thread at SO, three-byte unicode characters aren't that easy in Java. You have to convert it to two characters. Java provides you with a method to do this conversion, so your source can look like this:

public static void main(String[] args) {
    String s1 = "Hello World! ";
    char c1 = '\u2661';
    char[] c2 = Character.toChars(0x1F427);
    write (s1);
    write (c1);
    write (new String(c2));
}
Lothar
  • 5,323
  • 1
  • 11
  • 27
  • An alternative is `String.format("%s%s%c", s, c1, 0x1f427)`. – VGR Oct 29 '17 at 15:41
  • Thank you for your answer, when I am trying your first code there appears just "Hello World!" and then in a new window there appears a number and then in a new window again nothing. I don`t know what is happening. And for you second code, what do you mean with the "s," in the parenthesis? D0n`t I need to introduce a variable first for this? – Z.Wo Oct 29 '17 at 16:44
  • I don't know what `write` actually is. I assumed that it's something like `Writer.write` where multiple calls of write shouldn't be different from your call where you concatenated these values. It doesn't seem to be the case, so instead of my code you might try `write(s1 + c1 + new String(c2))` – Lothar Oct 29 '17 at 16:49
  • Thank you for your comment, it is not working either. There just does`t appear a penguin. Isn`t there a possibility without arrays? – Z.Wo Oct 29 '17 at 18:58
  • Have you tried `"\uD83D\uDC27"` as suggested by others in the comments? If that doesn't bring up the penguin, either, the problem then resides in the code of the presentation (i.e. the `write` method) rather than the code presented here. There is no workaround for the char-array, see the description in the other SO-article that was linked to in the comments to your question – Lothar Oct 29 '17 at 19:01
  • Yes I tried this one with a String, it does`t show the penguin either. I am getting crazy here. I tried everything. How is this possible? – Z.Wo Oct 29 '17 at 19:20
  • When I am trying this one String c3 = "♡"; it works but when I am trying String c2=""; it shows nothing - isn`t it strange? Can the problem be some default settings in eclipse? – Z.Wo Oct 29 '17 at 19:31
  • Right click onto the file and select Properties. This opens a dialog that allows you to set the charset being used to save the file. If it isn't UTF8 or some other Unicode-charset, the character will get lost when saving. – Lothar Oct 29 '17 at 19:42