[java] How to use readline() method in Java?

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?

This question is related to java io readline

The answer is


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){
        }
    }
}

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.


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);

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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to io

Reading file using relative path in python project How to write to a CSV line by line? Getting "java.nio.file.AccessDeniedException" when trying to write to a folder Exception: Unexpected end of ZLIB input stream How to get File Created Date and Modified Date Printing Mongo query output to a file while in the mongo shell Load data from txt with pandas Writing File to Temp Folder How to get resources directory path programmatically ValueError : I/O operation on closed file

Examples related to readline

Read line with Scanner Python Serial: How to use the read or readline function to read more than 1 character at a time Getting rid of \n when using .readlines() How to use readline() method in Java? Convert InputStream to BufferedReader Why do I get the "Unhandled exception type IOException"? Best method for reading newline delimited files and discarding the newlines?