如何在Java中使用readline()方法?

发布于 2024-12-22 00:34:05 字数 882 浏览 4 评论 0原文

我是 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

我想问以下问题:

  1. 下面的语句做了什么?

    DataInputStream in = new DataInputStream(System.in);
    

    如果 inDataInputStream 的对象,那么有什么新内容以及上面语句右侧的语句的作用是什么?

  2. 为什么将整数值放入 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:

  1. What is done in the following statement?

    DataInputStream in = new DataInputStream(System.in);
    

    If in is an object of DataInputStream then what is new and what do the statement on the right-hand side of above statement do?

  2. Why have different methods been used for putting the integer value into intnumber and float value into floatnumber?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紅太極 2024-12-29 00:34:05

我建议您使用 Scanner 而不是 DataInputStreamScanner 是专门为此目的而设计的,并在 Java 5 中引入。请参阅以下链接以了解如何使用 Scanner

示例

Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());

I advise you to go with Scanner instead of DataInputStream. Scanner is specifically designed for this purpose and introduced in Java 5. See the following links to know how to use Scanner.

Example

Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
も让我眼熟你 2024-12-29 00:34:05

使用 BufferedReader 和 InputStreamReader 类。

BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String line=buffer.readLine();

或者使用 java.util.Scanner 类方法。

Scanner scan=new Scanner(System.in);

Use BufferedReader and InputStreamReader classes.

BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String line=buffer.readLine();

Or use java.util.Scanner class methods.

Scanner scan=new Scanner(System.in);
青衫负雪 2024-12-29 00:34:05

DataInputStream 只是 InputStream(即 System.in)上的装饰器,它允许使用更方便的方法进行读取。

至于 Float.valueOf() ,这很奇怪,因为 Float 也有 .parseFloat() 。此处,代码使用 .valueOf() 获取 Float,并使用 .floatValue() 将其转换为原始 float 类型code>,由于自动拆箱,对于 Java 1.5+ 来说这是不必要的。

正如其他答案正确地说的那样,这些方法无论如何都已经过时了。

A DataInputStream is just a decorator over an InputStream (which System.in is) which allows to read using more convenient methods.

As to the Float.valueOf(), well, that's curious because Float has .parseFloat() as well. Here the code grabs a Float with .valueOf() which it turns into the primitive float 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.

浪推晚风 2024-12-29 00:34:05

总之:我会小心你复制的代码。您可能正在复制恰好可以工作的代码,而不是精心选择的代码。

在intnumber中,使用parseInt,在floatnumber中使用valueOf,为什么会这样呢?

我看不出有什么充分的理由。正如您所怀疑的那样,这是对 API 的不一致使用。


Java 区分大小写,并且没有任何 Readline() 方法。也许你的意思是readLine()。

DataInputStream.readLine() 已弃用,转而使用 BufferedReader.readLine();

但是,对于您的情况,我将使用 Scanner 类。

Scanner sc = new Scanner(System.in);
int intNum = sc.nextInt();
float floatNum = sc.nextFloat();

如果您想知道类的作用,我建议您快速查看 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.

In intnumber, parseInt is used and in floatnumber valueOf is used why so?

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.

Scanner sc = new Scanner(System.in);
int intNum = sc.nextInt();
float floatNum = sc.nextFloat();

If you want to know what a class does I suggest you have a quick look at the Javadoc.

半夏半凉 2024-12-29 00:34:05

我想这可以解释它......

import java.io.*;

class reading
{
    public static void  main(String args[]) throws IOException
    {
        float number;
        System.out.println("Enter a number");
        try
        {
            InputStreamReader in = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(in);
            String a = br.readLine();
            number = Float.valueOf(a);
            int x = (int)number;

            System.out.println("Your input=" + number);
            System.out.println("Your input in integer terms is = " + x);
        }
        catch(Exception e){
        }
    }
}

This will explain it, I think...

import java.io.*;

class reading
{
    public static void  main(String args[]) throws IOException
    {
        float number;
        System.out.println("Enter a number");
        try
        {
            InputStreamReader in = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(in);
            String a = br.readLine();
            number = Float.valueOf(a);
            int x = (int)number;

            System.out.println("Your input=" + number);
            System.out.println("Your input in integer terms is = " + x);
        }
        catch(Exception e){
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文