theProgrammer101 theProgrammer101 - 5 months ago 17
Java Question

How can I print unicode symbols within a certain range?

I am trying to make a program which prints all unicode symbols \u6000 through \u7000. My program prints 50 characters, starts a new line, prints 50 more, etc. (no issue here). I know how to print unicode symbols, but I am not sure how to print them incrementally(adding 1 each time). Here is my program:

public class UnicodePrinter {
public static void main(String args[]) {
for (int i = 6000; i<7000; i++) {
if(i%50 == 0) {
System.out.println();
}
System.out.print("\u"+i); //issue here, see below
}
}
}


I get an error here, saying "Invalid unicode", but I have no idea how to fix it. Thank you in advance to any helpers.

Answer Source

Just cast it to a char after converting it to a hex:

for (int i = 6000; i<7000; i++) {
    if(i%50 == 0) {
        System.out.println();
    }
    char c = (char) Integer.parseInt(String.valueOf(i), 16);
    System.out.print(c);
}