3

I'm trying to store Japanese words as strings, but every time I try to store the strings I get question marks where the characters should be.

for example if the user enters: こんにちは

String string = scan.next();
System.out.println(string);

It will display five question marks, where the five characters should display. If I declare a Japanese word as a string though I can display the word.

ex.

 String Kana = "こんにちは";
 System.out.println(Kana);

will display こんにちは.

I'm having a similar problem with JLabels, I get question marks when I send it a user entered word, but when I set the text using a word I set it works. I have the font to display Japanese text, so I'm at a dead end.

I believe something is happening to the text once the user enters the word, but I can't figure out what. I'm also using netbeans and source file is coded in unicode, so I don't think that this is the problem.

Here is full code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.awt.*;
import java.io.*;
import java.nio.charset.Charset;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 *
 * @author tyler.stanley.4937
 */
public class JavaApplication1 extends JFrame {
    /**
     * @param args the command line arguments
     */
    private final Charset UTF8_CHARSET = Charset.forName("UTF-8");
    File fontFile = new File("MSMINCHO.TTF");
    Font baseFont = null;
    Scanner scan=new Scanner(System.in,"UTF-8");
    JLabel japanese = new JLabel("Hello");

    public JavaApplication1(){
        //Locale.setDefault(new Locale("ja"));
        setSize(300,300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        JPanel panel = new JPanel(new FlowLayout());
        contentPane.add(panel);
        panel.add(japanese);
    }
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, FontFormatException {
        // TODO code application logic here
        JavaApplication1 ja = new JavaApplication1();
        ja.start();
    }
    public void start() throws FileNotFoundException, IOException, ClassNotFoundException, FontFormatException{
        //Locale bLocale = Locale.forLanguageTag("ja-JP-u-ca-japanese");


        InputStream fontFileInputStream = new FileInputStream(fontFile);
        baseFont = Font.createFont(Font.TRUETYPE_FONT, fontFileInputStream);
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        font = font.deriveFont(Font.PLAIN, 14f);

        System.out.println("Enter Kanji");
        String string = new String(scan.next());
        StringBuffer sb = new StringBuffer(string);

        japanese.setFont(baseFont.deriveFont(Font.PLAIN, 24));
        japanese.setText(sb.toString());
        System.out.println("Enter Romanji");
        String Romanji = scan.next();
        System.out.println("How common is it");
        int common = scan.nextInt();
        System.out.println("How many types of word is it?");
        int loop = scan.nextInt();
        //List<int> typeOfWord = new ArrayList<int>();
        ArrayList type = new ArrayList();
        for(int i = 0; i<loop;i++){
            System.out.println("What type of word");
            type.add(scan.nextInt());
        }
        System.out.println("What type of adjective");
        int adjective = scan.nextInt();
        System.out.println("What type of verb");
        int verb = scan.nextInt();
        System.out.println("How many radicals");
        int loop2 = scan.nextInt();
         ArrayList radical = new ArrayList();
        Word word = new Word(sb.toString(),Romanji,common,type,adjective,verb,radical);
        word.getKanaKanji();// gives ?????
        store(word);

        read();

    }
    public void store(Word word) throws FileNotFoundException, IOException, FontFormatException{
        File file = new File("test.doc");
        File newfile = new File ("kanji.rtf");

        FileOutputStream ofs = new FileOutputStream(newfile);
        PrintWriter print = new PrintWriter(ofs);
        System.out.println("Add Kanji");
        String kanji = scan.next();
        print.write(kanji);
        print.close();


        FileOutputStream outFileStream = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(outFileStream);
        oos.writeObject(word);
        oos.close();
    }
}

When I open the file that I wrote the kanji to, it does the same thing by displaying a bunch of question marks. The amount of question marks add up to the amount of characters, but they just won't display if I use a scanner or any other method to get text from the user.

Taikun
  • 55
  • 1
  • 5
  • 2
    it's problem with your shell display in the first case.... that might not support all characters.....there is no serious problem from your side – pinkpanther Jun 04 '13 at 20:32
  • @pinkpanther: I doubt his first println("string") will print the correct answer ;) The "" are wrong there. It should be println(string) – Burkhard Jun 04 '13 at 20:36
  • @Burkhard i'm talking about his context not that println – pinkpanther Jun 04 '13 at 20:38
  • change the character encoding of your IDE that is set to os settings by default.....change that one – pinkpanther Jun 04 '13 at 20:47
  • Your question has an answer [http://stackoverflow.com/questions/15488628/chinese-characters-displayed-as-questions-marks-in-mac-terminal](here) – pinkpanther Jun 04 '13 at 20:49

1 Answers1

0

Well, for printing in standard output change the encoding setting of netbeans or create a UTF-8 stream explicitly.

Part1:

The first part taken from an answer here

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class JavaTest {

    public static void main(String[] args) {
        try{
            PrintStream out = new PrintStream(System.out, true, "UTF-8");
            String Kana = "こんにちは";
            out.println(kana);

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

Part2: Well, for displaying in JLabel call the JLabel#setFont method passing the required font name available on your system for japanese. For more information refer to javadocs of Font class.

Example:

 JLabel jl=new JLabel();
 jl.setFont(new Font(yourFontString,20,20));
 jl.setText(Kana);

Edit: If you have troubles with your Scanner

use public Scanner(InputStream source,String charsetName)

Example: Scanner scan=new Scanner(System.in,"UTF-8");

EDIT:

When I open the file that I wrote the kanji to, it does the same thing by displaying a bunch of question marks. The amount of question marks add up to the amount of characters, but they just won't display if I use a scanner or any other method to get text from the user.

You are wrapping FileOutputStream in PrintWriter. Instead of doing this, you can wrap in in a OutputStreamWriter, because it provides a way configuring encoding through its constructor. You have used similar method for writing your word object.

Ex:



    File newfile = new File ("kanji.rtf");
    FileOutputStream ofs = new FileOutputStream(newfile);
    OutputStreamWriter print = new OutputStreamWriter(ofs,YOURCHARSET);

When reading the kanji.rtf file do the same in reverse with InputStream or Scanner class and use the same charset you have used to write the file.

Community
  • 1
  • 1
pinkpanther
  • 4,770
  • 2
  • 38
  • 62