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.