Scanner vs System.console()。write() [英] Scanner vs System.console( ).write( )

查看:96
本文介绍了Scanner vs System.console()。write()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在屏幕上打印出包含西班牙语重音的字符串。
我用相同的输入得到不同的答案,但采用不同的方法。

I tried to print out a string that includes Spanish accents on the screen. I get different answer with the same input, but with different approaches.

我的第一种方法是通过扫描仪课程读取包含西班牙语口音的字符串

My first approach is to read a string that includes Spanish accents via Scanner class

 //input is a á e é i í o ó u ú A Á E É I Í O Ó U Ú Ñ ñ

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter some spanish chars next: ");
    String spanishLine = sc.nextLine();

    try {
        System.out.println("Output from Scanner: " + spanishLine);
        System.console().writer().println(spanishLine);
    } catch (Exception e) {

    }

我的第二个apporche是我从这个网站找到的: http://www.rgagnon.com/javadetails /java-0046.html

My second apporche is which I found from this webiste: http://www.rgagnon.com/javadetails/java-0046.html

        String s = "a á e é i í o ó u ú A Á E É I Í O Ó U Ú Ñ ñ";

        try {
            System.out.println("Output from System.console: " + s);
            System.console().writer().println(s);
        } catch (Exception e) {

        }

我的out come如下

My out come is as follows

Enter some spanish chars next: 
a á e é i í o ó u ú A Á E É I Í O Ó U Ú Ñ ñ
output from Scanner: a � e � i � o � u � A � E � I � O � U � � �
output from System.console: a á e é i í o ó u ú A Á E É I Í O Ó U Ú Ñ ñ

任何人都可以解释为什么我从同一输入中得到两个不同的答案。
如何修复我的第一种方法以获得与第二种方法相同的结果?

Can anyone explain why I get two different answers from the same input. How can I fix up my first approach to have same result as the second one?

推荐答案

不是答案

Not an answer

你还需要检查一些东西。

You still need to check some things.


  1. java编译器使用的java源代码编码必须与文本/编辑器的编码相同。

  1. The java source encoding used by the java compiler must be the same as the encoding of the text/editor.


  • 您可以使用程序员的编辑器(如JEdit或NotePad ++)进行检查。

检查默认编码:


  • System.out.println(System.getProperty(file.encoding));

  • 或者向扫描器构造函数添加额外的编码参数

  • 原因是,通常有一个没有编码的方法/构造函数版本采用默认的平台编码。

将System.out / console重定向到文件。

Redirect the System.out/console to a file.


  • 在这种情况下,将字符串写入UTF就足够了-8。

尝试真正的控制台,而不是IDE的。


  • IDE控制台的编码可以配置。

我发现行为令人费解。它看起来好像Scanner采用编码UTF-8的字符集然后必须将其写入US-ASCII,因此umappable字符由 给出。那不可能。看起来Scanner会马车 - 我怀疑。

I find the behaviour puzzling. It would look as if the Scanner took a charset encoding UTF-8 and then had to write it to US-ASCII, so umappable characters are given by . That cannot be. It looks like Scanner would be "buggy" - which I doubt.

尝试转储字符串:

for (int i = 0; i < s.length(); ) {
    int cp = s.codePointAt(i);
    System.out.printf(" %x", cp);
    i += Character.charCount(cp);
}
System.out.println();






(通常没有完成) )


(Normally not done)

System.setProperty("file.encoding","Windows-1252");

这篇关于Scanner vs System.console()。write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆