Java System.in.read() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22708071/
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
System.in.read() method
提问by Ashish Biyahut
Following is the code, I have written to get two inputs from user. But when I run the program, It takes only one input and generate other by itself and calculate the wrong value. please help. Thanks
以下是我编写的代码,用于从用户那里获取两个输入。但是当我运行程序时,它只需要一个输入并自行生成另一个并计算错误的值。请帮忙。谢谢
import java.io.IOException;
import java.util.*;
class ThrowsExcpt {
int divide(int x, int y) throws ArithmeticException, IOException {
return x / y;
}
}
class ThrowsTemps {
public static void main(String s[]) throws IOException {
int x = System.in.read();
int y = System.in.read();
ThrowsExcpt th = new ThrowsExcpt();
int r = th.divide(x, y);
System.out.println(r);
}
}
回答by Savv
Try using a Scanner object to read your user's input :
尝试使用 Scanner 对象来读取用户的输入:
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
回答by matt
System.in
is an InputStream
- read()
reads exactly one byte. Your direct input is more than one byte and so both values are directly read within the first input.
System.in
是一个InputStream
-read()
正好读取一个字节。您的直接输入不止一个字节,因此在第一个输入中直接读取两个值。
Use a Scanner instead (with System.in as Input):
改用扫描仪(使用 System.in 作为输入):
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
More Examples: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
更多示例:http: //docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
回答by JavaLearner
Read()method.
读取()方法。
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
从输入流中读取下一个字节的数据。值字节以 0 到 255 范围内的 int 形式返回。如果由于已到达流末尾而没有可用字节,则返回值 -1。此方法会阻塞,直到输入数据可用、检测到流结束或抛出异常为止。
from http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29.
来自http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29。
So use scanner
for this.
所以用scanner
这个。
And according to your question Why is it not asking for second input?Reason is read() method wait for input source once it gets the input it takes the byte by byte from that input source. For ex:
根据您的问题,为什么不要求第二次输入?原因是 read() 方法在获得输入后等待输入源,它从该输入源中逐字节获取。例如:
inChar = System.in.read();
i = System.in.read();
i1 = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
System.out.println(i);
System.out.println(i1);
you enter abc
as input then you will get
你输入abc
作为输入然后你会得到
Enter a Character:
abc
You entered 97
98
99
as out put which is byte values of a,b and c.
作为输出,它是a,b 和 c 的字节值。
Explanation:input abc
byte array
说明:输入abc
字节数组
+--------------------+
| 97 | 98 | 99 | other byte value for **Enter**
+--------------------+
first System.in.read() will get first index of array, second one will get second and so on.
第一个 System.in.read() 将获得数组的第一个索引,第二个将获得第二个,依此类推。
回答by HelloWorld
hey bro this should work for what you need i think if its not let me know
嘿兄弟这应该可以满足你的需要我认为如果它不让我知道
import java.io.IOException;
import java.util.Scanner;
public class noob{
class ThrowsExcpt {
int divide(int x, int y) throws ArithmeticException, IOException {
return x / y;
}
}
class ThrowsTemps {
public void main(String s[]) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number 1");
int x = sc.nextInt();
System.out.println("Enter number 2");
int y = sc.nextInt();
回答by Tapper7
simply put -
简单的说 -
int x = System.in.read();
automatically throws an exception.
自动抛出异常。
- you allocated and defined a memory cell to hold an int, then assigned a byte to be stored there. Recast the input or use a diff. input method.
- 您分配并定义了一个内存单元来保存一个 int,然后分配一个字节存储在那里。重铸输入或使用差异。输入法。
回答by gaidink
Why unwanted assignment is occuring?
为什么会发生不需要的分配?
The keyboard input character(s) is stored in input buffer after ENTER is pressed. ENTER includes line feed '\n' in the input character. Decimal/integer value of line feed '\n' is 10.
按下 ENTER 后,键盘输入的字符将存储在输入缓冲区中。ENTER 在输入字符中包含换行符 '\n'。换行符 '\n' 的十进制/整数值为 10。
System.in.read() reads only first one character from the input character(s), the character is casted into integer and return it. And that character will be excluded from the input buffer.
System.in.read() 只从输入字符中读取第一个字符,该字符被转换为整数并返回它。并且该字符将从输入缓冲区中排除。
The rest of the characters are still pending in the input buffer until it is read by System.in.read() in the same manner stated above.
其余字符仍在输入缓冲区中等待,直到 System.in.read() 以上述相同方式读取为止。
int inputchar1, inputchar2, inputchar3;
System.out.print("input: ");// input: 43
// The line feed '\n' is generated when ENTER is pressed.
// '4', '3' and '\n' are in the input buffer.
inputchar1 = System.in.read(); // '4' is read and returns 52 (corresponding integer value)
// '3' and '\n' is in the input buffer.
inputchar2 = System.in.read(); // '3' is read and returns 51
// '\n' is pending in the input buffer.
inputchar3 = System.in.read(); // '\n' is read and returns 10
// no more input character in the input buffer.
System.out.println("inp1 =" + inputchar1);
System.out.println("inp1 =" + inputchar2);
System.out.println("inp1 =" + inputchar3);
/*
* Refer to ASCII table for char to decimal conversion.
* Although java Char is 16-bit unicode ASCII is still applicable.
*/
How to avoid unwanted assignment?
如何避免不必要的分配?
Keep reading the content of input buffer until it gets depleted.
继续读取输入缓冲区的内容,直到它耗尽。
int x, y, avoid;
System.out.println("Enter x: ");
x = System.in.read();
// Reading all the characters after the first character so that
// no character stays pending in the input buffer.
do {
avoid = System.in.read();
} while(avoid != '\n');
System.out.println("Enter y: ");
// New input buffer is created.
y = System.in.read();
do {
avoid = System.in.read();
} while(avoid != '\n');
Reference: "Java: A beginner's guide 6e" - Herbert Schildt
参考:“Java:初学者指南 6e” - Herbert Schildt