gpt4 book ai didi

java - 使用日语输入java

转载 作者:行者123 更新时间:2023-12-02 06:53:56 25 4
gpt4 key购买 nike

我试图将日语单词存储为字符串,但每次我尝试存储字符串时,我都会在字符应该在的位置出现问号。

例如,如果用户输入:こんにちは

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

它将显示五个问号,其中应显示五个字符。如果我将日语单词声明为字符串,尽管我可以显示该单词。

例如。

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

将显示こんにちは

我在使用JLabels时遇到了类似的问题,当我向它发送用户输入的单词时,我收到问号,但是当我使用单词设置文本时,我将其设置为有效。我有显示日语文本的字体,所以我陷入了死胡同。

我相信一旦用户输入单词,文本就会发生一些变化,但我不知道是什么。我也在使用 netbeans 并且源文件是用 unicode 编码的,所以我不认为这是问题所在。

完整代码如下:

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

当我打开写入汉字的文件时,它会显示一堆问号来执行相同的操作。问号的数量加起来就是字符的数量,但如果我使用扫描仪或任何其他方法从用户那里获取文本,它们就不会显示。

最佳答案

好吧,为了在标准输出中打印,请更改 netbeans 的编码设置或显式创建 UTF-8 流。

第 1 部分:

第一部分取自答案 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();
}
}
}

第二部分:那么,为了在 JLabel 中显示,请调用 JLabel#setFont 方法,传递系统上可用的日文所需字体名称。有关详细信息,请参阅 Font 类的 javadocs

示例:

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

编辑:如果您的扫描仪出现问题

使用公共(public)扫描仪(InputStream源,字符串字符集名称)

示例:扫描仪 scan=new Scanner(System.in,"UTF-8");

编辑:

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.

您正在将 FileOutputStream 包装在 PrintWriter 中。您可以将其包装在 OutputStreamWriter 中,而不是这样做,因为它提供了一种通过其构造函数配置编码的方法。您已使用类似的方法来编写 word 对象。

Ex:



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

当读取kanji.rtf文件时,使用InputStreamScanner类进行相反的操作,并使用您以前使用过的相同字符集写入文件。

关于java - 使用日语输入java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com