如何在Java中使用readline()方法?
我是 Java 初学者,我正在阅读通过键盘上的 readLine() 方法为变量赋值的主题。书中给出的程序如下:
import java.io.DataInputStream
class Reading
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
int intnumber=0;
float floatnumber=0.0f;
try {
system.out.println("enter an integer: ");
intnumber = Integer.parseInt(in.readline());
system.out.println("enter a float number: ");
floatnumber = Float.valueOf(in.readline()).floatvalue();
}
// Rest of code
我想问以下问题:
下面的语句做了什么?
DataInputStream in = new DataInputStream(System.in);
如果
in
是DataInputStream
的对象,那么有什么新内容以及上面语句右侧的语句的作用是什么?为什么将整数值放入 intnumber 和将浮点值放入 floatnumber 使用不同的方法?
I am beginner in Java, and I was reading the topic of giving values to variables through the readLine()
method from the keyboard. The program for that is given in the book is as follows:
import java.io.DataInputStream
class Reading
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
int intnumber=0;
float floatnumber=0.0f;
try {
system.out.println("enter an integer: ");
intnumber = Integer.parseInt(in.readline());
system.out.println("enter a float number: ");
floatnumber = Float.valueOf(in.readline()).floatvalue();
}
// Rest of code
I want to ask the following questions:
What is done in the following statement?
DataInputStream in = new DataInputStream(System.in);
If
in
is an object ofDataInputStream
then what is new and what do the statement on the right-hand side of above statement do?Why have different methods been used for putting the integer value into intnumber and float value into floatnumber?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您使用
Scanner
而不是DataInputStream
。Scanner
是专门为此目的而设计的,并在 Java 5 中引入。请参阅以下链接以了解如何使用Scanner
。示例
I advise you to go with
Scanner
instead ofDataInputStream
.Scanner
is specifically designed for this purpose and introduced in Java 5. See the following links to know how to useScanner
.Example
使用 BufferedReader 和 InputStreamReader 类。
或者使用 java.util.Scanner 类方法。
Use BufferedReader and InputStreamReader classes.
Or use
java.util.Scanner
class methods.DataInputStream
只是InputStream
(即System.in
)上的装饰器,它允许使用更方便的方法进行读取。至于 Float.valueOf() ,这很奇怪,因为 Float 也有 .parseFloat() 。此处,代码使用
.valueOf()
获取Float
,并使用.floatValue()
将其转换为原始float
类型code>,由于自动拆箱,对于 Java 1.5+ 来说这是不必要的。正如其他答案正确地说的那样,这些方法无论如何都已经过时了。
A
DataInputStream
is just a decorator over anInputStream
(whichSystem.in
is) which allows to read using more convenient methods.As to the
Float.valueOf()
, well, that's curious becauseFloat
has.parseFloat()
as well. Here the code grabs aFloat
with.valueOf()
which it turns into the primitivefloat
type using.floatValue()
, which is unnecessary with Java 1.5+ due to auto unboxing.And as other answers rightly say, these methods are obsolete anyway.
总之:我会小心你复制的代码。您可能正在复制恰好可以工作的代码,而不是精心选择的代码。
我看不出有什么充分的理由。正如您所怀疑的那样,这是对 API 的不一致使用。
Java 区分大小写,并且没有任何
Readline()
方法。也许你的意思是readLine()。DataInputStream.readLine() 已弃用,转而使用 BufferedReader.readLine();
但是,对于您的情况,我将使用 Scanner 类。
如果您想知道类的作用,我建议您快速查看 Javadoc。
In summary: I would be careful as to what code you copy. It is possible you are copying code which happens to work, rather than well chosen code.
There is no good reason I can see. It's an inconsistent use of the APIs as you suspect.
Java is case sensitive, and there isn't any
Readline()
method. Perhaps you mean readLine().DataInputStream.readLine() is deprecated in favour of using BufferedReader.readLine();
However, for your case, I would use the Scanner class.
If you want to know what a class does I suggest you have a quick look at the Javadoc.
我想这可以解释它......
This will explain it, I think...