Java 使用 BufferedReader 输入字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42640949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:16:56  来源:igfitidea点击:

Using BufferedReader to input a character

java

提问by Aditya Thakur

BufferedReader can be used to input integers, floats and so on

BufferedReader 可用于输入整数、浮点数等

import java.io.*;

public class Wrap {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int h = Integer.parseInt(br.readLine());

    }

}

Is there any way to enter characters using it?

有没有办法使用它输入字符?

采纳答案by JustCode

Try with this code

尝试使用此代码

public class Wrap {
    public static void main(String[] args) throws IOException {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         char c = (char)br.read();
         System.out.println(c);
}

}

}

回答by Rahul

use the readmethod

使用读取方法

 private static void handleCharacters(Reader reader)
            throws IOException {
        int r;
        while ((r = reader.read()) != -1) {
            char ch = (char) r;
            //process
        }
}

Here is how you can accumulate the chars in an array of size 100.

以下是如何在大小为 100 的数组中累积字符的方法。

     char charArray[] = new char[ 100 ];
     int i = 0;
     BufferedReader buffer = new BufferedReader(
         new InputStreamReader(System.in));
     int c = 0;
     while((c = buffer.read()) != -1 && i < 100) {
          char character = (char) c;
          charArray[i++] = c;
     }
     System.out.println(charArray);

回答by Ronak Poojara

char a=br.readLine().charAt(0);