- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将日语单词存储为字符串,但每次我尝试存储字符串时,我都会在字符应该在的位置出现问号。
例如,如果用户输入:こんにちは
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
文件时,使用InputStream
或Scanner
类进行相反的操作,并使用您以前使用过的相同字符集写入文件。
关于java - 使用日语输入java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926904/
我正在尝试在日本网站上的 flash 动态文本字段上实现抗锯齿。这些文本字段的内容不是固定数据。 我没有嵌入字体的选项,因为嵌入所有字符会导致非常重的 swf 文件(3000kb+ 仅用于字体)。这是
我正在创建一个游戏。我有一些带有文本的用户界面。最近我们想添加日语版本,但我遇到字体问题。我使用 stb_freetype 来光栅化字体,并且我支持 Unicode,所以这应该不是问题。但大多数字体似
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我知道我的问题已经在这里有了解决方案。但我只想具体说明我的情况。我有一个 json 对象,其中包含非英语字符。 例如。 {“my_chinise_name”:“吉米”}。 该对象将通过 javascr
我刚刚安装了 Tesseract OCR 并在运行命令 $ tesseract --list-langs 后输出仅显示 2 种语言,eng和osd 。我的问题是,如何加载另一种语言,在我的例子中是日语
在哪里可以获得与日语汉字、平假名和片假名字符对应的 ASCII 代码列表。我正在做一个java函数和Javascript来确定它是否是日语字符。它的 ASCII 码范围是多少? 最佳答案 ASCII代
在我的 Rails 应用程序中,我有一个像这样的文本字段 @disabled_field %> 我正在使用 jQuery 来验证此文本字段仅接受 half width katakana字符(Unic
我有在表单中添加网站选项的功能。 用户可以在这里写域/url,这个域/url 可以是英语也可以是日语,如下所示。 www.google.com www.南极星.com I am using follo
我遇到了日语问题。我有一个允许用户搜索数据的表格。当用户输入要搜索的字符串并按下“Enter”键时,搜索功能就会执行。我的代码是: $('#formSearch input').keyup(funct
我是 Java 脚本的新手。我写了一个正则表达式,它允许用户名或电子邮件地址作为某些条件的输入,例如应接受 6-50 个字符不允许空格或空白不应允许 2 个 @ 符号并且可以接受超过 1 个 .(点)
您好,我有一个包含日语文本的文件,保存为 unicode 文件。 我需要从文件中读取信息并将信息显示到标准输出。 我正在使用 Visual Studio 2008 int main() {
我正在使用在 http://mobile.tutsplus.com/tutorials/android/android-sdk-using-the-text-to-speech-engine/ 中找到
(我正在开发一个网站来抓取 ASP.NET 中的其他网站内容。我能够正确获取内容,但如何根据该内容识别使用的是哪种语言。例如英语、印地语、中文, 日语等 我使用了以下代码。 HttpWebReques
在 TextBox 中,我无法捕获具有特殊 IME 的语言(例如日语和韩语)的击键,因为 IME 不断地决定不同的字符或音节。实际上,大多数情况下 KeyUp/KeyDown 都不会触发(如果 IME
我有一个包含日语单词的数据库。我很困惑,因为这个查询: SELECT japanese FROM my_table where japanese = 'する' 返回两个结果: ずる する 我试图查看
我想将 CSV 文件解析为 JSON 文件。我已经解析了它,但它没有得到日语字符。 我正在使用 Papa Parser 将 CSV 解析为 JSON。 这是我的代码:- Papa.parse("htt
我有一个 C# winform 应用程序,它安装在日文 windows 7 上。一些标签以非常宽的字体显示,导致它们无法匹配 from 的大小。 经过一些研究,我被告知这可能是半角/全角问题。有没有办
MySql 全文搜索是否适用于非拉丁语言? (希伯来语、阿拉伯语、日语……) 添加:做了一些测试...希伯来语有一些问题。示例:名称 מוסəנזון 的发音与 מושəנזון 相同,但搜索不会找到
我使用下一个代码从句柄为 hFile 的文件中读取所有元素行得通,而且我用 GetFileSize(hFile, NULL) 得到了它的大小. _TCHAR* text = (_TCHAR*)mall
验证按预期进行,但消息仅以英语显示良好。我使用自己的日语消息。 问题是我收到了日语的乱码验证消息。我将validation-api-1.0.0.GA.jar与hibernate-validator-4
我是一名优秀的程序员,十分优秀!