java console input

前端 未结 2 1163
南旧
南旧 2021-01-22 04:34

The data type of the any input through console (as i do using BufferedReader class) is String.After that we type cast it to requered data type(as Inter.parseInt() for integer).B

相关标签:
2条回答
  • 2021-01-22 05:07

    Console input is actually read in as a series of bytes, not a String. This is because System.in is exposed by the API as an InputStream. The typical wrapping before JDK1.5 (hooray for the Scanner class!) was something like:

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    i.e. InputStreamReader converts the byte stream into a character stream and then the BufferedReader is used to do your readLine() operations, or whatever.

    So it's a String output because you're getting the buffered output of a character stream from your BufferedReader.

    0 讨论(0)
  • 2021-01-22 05:08

    In java you can do:

    Scanner scan = new Scanner(System.in);
    scan.nextInt();
    scan.nextDouble();
    

    etc. You just need to make sure the next input is correct.

    EDIT: Missed the Buffered Reader part. I think this answer is totally irrevelant.

    0 讨论(0)
提交回复
热议问题