繁体   English   中英

Eclipse stdin没有发送回车和换行

[英]Eclipse stdin without sending carriage return and line feed

我想通过标准输入(控制台)向我的程序发送输入,但是当我这样做时,它会再发送两个字符,Carriage Return和Line Feed。 我不希望我的程序读这些。 有没有办法可以发送我输入的内容而不发送这些字符?

这是我的代码:

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        while (true) {
            try {
                System.out.println(System.in.read());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

控制台输出:

a <- Here I typed 'a' and hit enter
97 <- These three came from stdout
13 <-
10 <-

您可以使用其中一个设计为一次读取一行的类,例如BufferedReader 这将拉入文本行,但解析行终止字符:

while (true) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(reader.readLine());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

请记住,由于这是拉入整行,输出将是一个String ,而不是逐字节。

是的你可以。 一种方法是替换它,

System.out.println(System.in.read());

char ch = (char) System.in.read();
if (ch != '\r' && ch != '\n') {
  System.out.println((int) ch);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM