Utilisation de java d'entrée japonais


J'essaie de stocker des mots japonais sous forme de chaînes, mais chaque fois que j'essaie de stocker les chaînes, j'obtiens des points d'interrogation où les caractères devraient être.

Par exemple, si l'utilisateur entre: こんにちは

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

Il affichera cinq points d'interrogation, où les cinq caractères devraient s'afficher. Si je déclare un mot japonais comme une chaîne, je peux afficher le mot.

Ex.

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

Affichera こんにちは.

J'ai un problème similaire avec JLabels, j'obtiens des points d'interrogation lorsque je l'envoie, un utilisateur a entré un mot, mais lorsque je définis le texte à l'aide d'un mot, cela fonctionne. J'ai la police pour afficher le texte japonais, donc je suis dans une impasse.

Je crois que quelque chose arrive au texte une fois que l'utilisateur entre le mot, mais je ne peux pas comprendre quoi. J'utilise également netbeans et le fichier source est codé dans unicode, donc je ne pense pas que ce soit le problème.

Voici le code complet:

/*
 * 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();
    }
}

Lorsque j'ouvre le fichier dans lequel j'ai écrit le kanji, il fait la même chose en afficher un tas de points d'interrogation. La quantité de points d'interrogation s'ajoute à la quantité de caractères, mais ils ne s'afficheront tout simplement pas si j'utilise un scanner ou toute autre méthode pour obtenir du texte de l'utilisateur.

Author: Taikun, 2013-06-05

1 answers

Eh bien, pour l'impression en sortie standard, modifiez le paramètre d'encodage de netbeans ou créez explicitement un flux UTF-8.

Part1:

La première partie pris d'une réponse ici

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: Eh bien, pour afficher dans JLabel appelez la méthode JLabel#setFont en passant le nom de police requis disponible sur votre système pour le japonais. Pour plus d'informations, reportez-vous à javadocs de Font classe.

Exemple:

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

Edit: Si vous avez des problèmes avec votre Scanner

Utiliser public Scanner(InputStream source,String charsetName)

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

MODIFIER:

Lorsque j'ouvre le fichier dans lequel j'ai écrit le kanji, il fait de même chose par afficher un tas de points d'interrogation. La quantité de points d'interrogation s'additionne à la quantité de caractères, mais ils ne s'afficheront tout simplement pas si je utiliser un scanner ou tout autre méthode pour obtenir du texte de l'utilisateur.

Vous enveloppez FileOutputStream dans PrintWriter. Au lieu de faire cela, vous pouvez envelopper dans un OutputStreamWriter, car il fournit un moyen de configurer l'encodage via son constructeur. Vous avez utilisé une méthode similaire pour écrire votre objet word.

Ex:



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

Lors de la lecture du fichier kanji.rtf faites la même chose en sens inverse avec la classe InputStream ou Scanner et utilisez le même jeu de caractères que vous avez utilisé pour écrire le fichier.

 0
Author: pinkpanther, 2017-05-23 12:21:53