Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display unicode characters in the Eclipse console window?

I am creating a small Java application that uses Unicode characters to create rows and columns of boxes in the console window in Eclipse Keplar. My code works completely fine but the output of each Unicode character I print is a small box instead of the Unicode character I want to print.

My code is as follows. I have two classes.

My main class:

package assign03;

import java.util.Scanner;

public class Assign03 {

private static int columns;
private static int cWidth;
private static int rows;
private static int rHeight;
private static Table table;

public static void main(String[] args) {
        table = new Table();
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of columns: ");
        columns = input.nextInt();

        System.out.print("Enter the width of the columns: ");
        cWidth = input.nextInt();

        System.out.print("Enter the number of rows: ");
        rows = input.nextInt();

        System.out.print("Enter the width of the rows: ");
        rHeight = input.nextInt();

        table.printFirstLine(columns, cWidth);

        for (int r = 0; r < rows; r++) {
            for (int rh = 0; rh < rHeight; rh++) {
                table.printVerticalLine(columns, cWidth);
            }
            if (r < (rows - 1)
                table.printInternalLine(columns, cWidth);
        }

        table.printLastLine(columns, cWidth);
        input.close();
    }
}

And my Table.java class:

package assign03;

public class Table {

    private char firstLine[] = { '\u2501', '\u250F', '\u2533', '\u2513' };
    private char internalLine[] = { '\u2501', '\u2523', '\u254B', '\u252B' };
    private char lastLine[] = { '\u2501', '\u2517', '\u253B', '\u251B' };
    private char verticalLine[] = { '\u2503' };
    private char whiteSpace[] = { '\u2003' };

    public void printFirstLine(int columns, int cWidth) {

        System.out.print(firstLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(firstLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(firstLine[2]);
        }

        System.out.println(firstLine[3]);
    }

    public void printVerticalLine(int columns, int cWidth) {
        for (int c = 0; c <= columns; c++) {
            System.out.print(verticalLine[0]);
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(whiteSpace[0]);
            }
        }
        System.out.println();
    }

    public void printInternalLine(int columns, int cWidth) {
        System.out.print(internalLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(internalLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(internalLine[2]);
        }

        System.out.println(internalLine[3]);
    }

    public void printLastLine(int columns, int cWidth) {

        System.out.print(lastLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(lastLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(lastLine[2]);
        }

        System.out.println(lastLine[3]);
    }
}

When the application runs, the output is as follows. I use inputs of 4 for columns, rows, column width, and row width.

Console Output

Any reason why my console output looks like that? Can anybody help me get the Unicode to display properly? Thanks.

like image 238
Benjamin Avatar asked Nov 18 '13 14:11

Benjamin


People also ask

How do I type Unicode characters in Eclipse?

Eclipse by default does not support Unicode / UTF-8. Turning it on, is very easy if you know where it is. At the bottom of that screen you can see 'Text file encoding'. Just choose the relevent one (choices range from US-ASCII to UTF-8), click the one you want, and presto, away you go.

How do I display Unicode?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.

How do I change from encoding to UTF-8 in Eclipse?

In Eclipse, go to Preferences>General>Workspace and select UTF-8 as the Text File Encoding. This should set the encoding for all the resources in your workspace.


2 Answers

The small box means that the current font doesn't have those symbols.

You will have to find a font which supports them and configure the console font (search the preferences for "console") to use it.

If it still doesn't work, make sure you have configured the Java application that you're running (i.e. which prints to the console) to emit UTF-8. In Eclipse, you can do that by opening the launch config and setting "Console Encoding" to UTF-8 (see this blog post or here)

like image 62
Aaron Digulla Avatar answered Nov 02 '22 04:11

Aaron Digulla


What worked for me was to go to Preferences, under Workspace, and change "Text file encoding" to UTF-8.

like image 30
Doradus Avatar answered Nov 02 '22 06:11

Doradus