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.
- Java Documentation
- Java Tutorial
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());
Answer from Jomoos on Stack OverflowJavatpoint
javatpoint.com โบ java-console-readline-method
Java Console readline() Method with Examples - Javatpoint
Java Console readline() Method with Examples on java console, java tutorial, java features, methods, java math, map, string, bufferreader, java vector, java concole format(), printf(), reader(), readline(), readpassword(), writer() etc.
Tutorialspoint
tutorialspoint.com โบ java โบ io โบ bufferedreader_readline.htm
Java - BufferedReader readLine() method
The Java BufferedReader readLine() method read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Videos
Tutorialspoint
tutorialspoint.com โบ java โบ io โบ console_readline.htm
Java - Console readLine() method
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = null; String name = null; try { // creates a console object console = System.console(); // if console is not null if (console != null) { // read line from the user input name = console.readLine("Name: "); // prints System.out.println("Name entered : " + name); } } catch(Exception ex) { // if any error occurs ex.printStackTrace(); } } }
Top answer 1 of 5
74
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.
- Java Documentation
- Java Tutorial
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());
2 of 5
32
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);
Javatpoint
javatpoint.com โบ how-to-read-file-line-by-line-in-java
How to read file line by line in Java - Javatpoint
How to read file line by line in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
Scaler
scaler.com โบ home โบ topics โบ readline() in java
readLine() in Java - Scaler Topics
August 5, 2022 - The readLine() method of BufferedReader class is used to read a one line text from a given input source for example, a file.
Tutorialspoint
tutorialspoint.com โบ java โบ io โบ console_readline_formatted.htm
Java - Console readLine(String fmt, Object... args) method
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = null; String username = "User"; String alpha = null; try { // creates a console object console = System.console(); // if console is not null if (console != null) { // read line from the user input alpha = console.readLine("%s!
GeeksforGeeks
geeksforgeeks.org โบ java โบ console-readline-method-in-java-with-examples
Console readLine() method in Java with Examples - GeeksforGeeks
June 12, 2020 - Below programs illustrate readLine() method in Console class in IO package: Program 1: ... // Java program to illustrate // Console readLine() method import java.io.*; public class GFG { public static void main(String[] args) { // Create the console object Console cnsl = System.console(); if (cnsl == null) { System.out.println( "No console available"); return; } // Read line String str = cnsl.readLine( "Enter string : "); // Print System.out.println( "You entered : " + str); } }
Tutorialspoint
tutorialspoint.com โบ java โบ io โบ randomaccessfile_readline.htm
Java - RandomAccessFile readLine() method
The following example shows the usage of RandomAccessFile readLine() method. package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { RandomAccessFile raf = new RandomAccessFile("lines1.txt", "rw"); // Write lines to the file raf.writeBytes("Hello\n"); raf.writeBytes("World\n"); // Reset file pointer to beginning raf.seek(0); // Read lines String line1 = raf.readLine(); String line2 = raf.readLine(); System.out.println("Line 1: " + line1); // Hello System.out.println("Line 2: " + line2); // World raf.close(); } catch (IOException e) { e.printStackTrace(); } } }
JournalDev
journaldev.com โบ 709 โบ java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - In this article, you will learn ... java.util.Scanner, Files.readAllLines(), and java.io.RandomAccessFile. You can use the readLine() method from java.io.BufferedReader to read a file line-by-line to String....
Java-samples
java-samples.com โบ showtutorial.php
readLine() to read strings from console input - Java samples
// A tiny editor. import java.io.*; class TinyEdit { public static void main(String args[]) throws IOException { // create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str[] = new String[100]; System.out.println("Enter lines of text."); System.out.println("Enter 'stop' to quit."); for (int i = 0; i < 100; i++) { str[i] = br.readLine(); if (str[i].equals("stop")) break; } System.out.println("\\nHere is your file:"); // display the lines for (int i = 0; i < 100; i++) { if (str[i].equals("stop")) break; System.out.println(str[i]); } } }
Naukri
naukri.com โบ code360 โบ library โบ readline-in-java
Readline in Java - Naukri Code 360
Almost there... just a few more seconds
Oracle
docs.oracle.com โบ javase โบ 7 โบ docs โบ api โบ java โบ io โบ BufferedReader.html
BufferedReader (Java Platform SE 7 )
public String readLine() throws IOException ยท Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Returns: A String containing the contents of the line, not including ...
Quora
quora.com โบ What-is-readLine-in-Java
What is readLine () in Java? - Quora
Answer: The read line() method of console class is used to read a single line of text from the console. the read line represents in public string read line(). this procedure returns the string hold the line read from the console. this function is used in java program to gather input for the progr...
California State University, Bakersfield
csub.edu โบ ~ychoi2 โบ MIS 260 โบ NotesJava โบ chap10 โบ ch10_13.html
readLine()
The program reads a line of character data from the keyboard by using the method readLine(), which is part of the BufferedReader object. ... It gets a line of characters, and assign them to the String inData. Then the next statement ... Enter the data: To learn Java, you must play with the programs!
GeeksforGeeks
geeksforgeeks.org โบ java โบ bufferedreader-readline-method-in-java-with-examples
BufferedReader readLine() method in Java with Examples - GeeksforGeeks
May 28, 2020 - // Java program to illustrate // BufferedReader readLine() method import java.io.*; public class GFG { public static void main(String[] args) { // Read the stream 'demo.txt' // containing text // "GEEKSFORGEEKS" // "geeksforgeeks" FileReader fileReader = new FileReader( "c:/demo.txt"); // Convert fileReader to // bufferedReader BufferedReader buffReader = new BufferedReader( fileReader); while (buffReader.ready()) { System.out.println( buffReader.readLine()); } } }