Since Java 8, you can read all lines into a Stream<String>:

private static String getInput() {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    return reader.lines().collect(Collectors.joining("\n"));
}

If you want to keep the newlines, you can use an overload for Collectors.joining(), like Collectors.joining("\n")

The problem with interactive input is: You may not know if there is an other line coming. So you have to signal end-of-file using your operating system input.
On windows this is the sequence Ctrl+Z, while on Linux it's Ctrl+D

Answer from Johannes Kuhn on Stack Overflow
🌐
HackerRank
hackerrank.com › challenges › java-stdin-and-stdout-1 › problem
Java Stdin and Stdout I | HackerRank
One popular way to read input from stdin is by using the Scanner class and specifying the Input Stream as System.in.
Discussions

java - getting input from stdin - Stack Overflow
I want to get input from stdin in the for of 3 10 20 30 the first number is the amount of numbers in the second line. Here's what I got, but it is stuck in the while loop... so I believe. I ran in... More on stackoverflow.com
🌐 stackoverflow.com
[JAVA]How do I read a continuous input from STDIN?
Put your data in a file. Redirect that file to standard input when you run the program. % java myprog.class < data.txt or whatever. More on reddit.com
🌐 r/learnprogramming
13
3
October 20, 2016
How to read "raw" standard input to a String
String str1 = scanner.nextLine(); Unless I’m mistaken this should convert the whole input into a string More on reddit.com
🌐 r/javahelp
9
1
October 3, 2019
[JAVA] Why does this code wait for input?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
27
1
November 29, 2023
🌐
Princeton CS
introcs.cs.princeton.edu › java › stdlib › javadoc › StdIn.html
StdIn
StdIn is a set of static methods and reads reads input from only standard input. It is suitable for use before a programmer knows about objects.
🌐
Study.com
study.com › computer science courses › computer science 109: introduction to programming
Standard Input Methods in Java - Lesson | Study.com
December 6, 2023 - Just like we have an output stream ... to read from the console. The top, abstract class in Java to represent an input stream of bytes is InputStream. This class is used in the System class to define an attribute (a field) called in to represent the ''standard'' input stream. Note that a common abbreviation for standard input is ''stdin....
🌐
EnableGeek
enablegeek.com › home › stdin java : input methods in java explained
Stdin JAVA : Input Methods in Java Explained - EnableGeek
April 2, 2024 - Using Console: The Console class provides a way to read input data from the console using stdin. We can create an instance of the Console class using the System.console() method, like this: ... Note: The Console class is not available in all environments, so it may not be suitable for all Java programs.
🌐
Talkerscode
talkerscode.com › howto › how-to-read-input-from-stdin-in-java.php
How To Read Input From Stdin In Java - TalkersCode.com
Then here we asking float type input from user. Even if you are giving integer type input that will treated as float type data. At last we printed user input with some message. //Read Stdin Input Example package java_prgms; import java.util.*; class RdSin_prgm { public static void main(String args[]) { Scanner sobj=new Scanner(System.in); System.out.println("Enter Float stdinput"); float f=sobj.nextFloat(); sobj.close(); System.out.println("stdinput value is "+f); } }
🌐
InterviewBit
interviewbit.com › problems › stdin-and-stdout
Stdin and Stdout | InterviewBit
One popular way to read input from stdin is by using the Scanner ( https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html ) class and specifying the Input Stream as System.in.
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-read-stdin-in-java-502562
How to read stdin in Java | LabEx
This tutorial explores various techniques for reading standard input in Java, providing developers with comprehensive strategies to handle console input effectively. Java offers multiple approaches to stdin processing, enabling programmers to choose the most suitable method for their specific programming requirements. Standard input (stdin) is a fundamental concept in Java programming that allows programs to receive input from ...
🌐
HackerRank
hackerrank.com › challenges › java-stdin-stdout › problem
Java Stdin and Stdout II | HackerRank
Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and ...
🌐
OneCompiler
onecompiler.com › java › 3y2ntjvc7
Read input from stdin: Buffered Reader - Java - OneCompiler
import java.util.*; import java.io.*; public class Main { public static void main(String[] args){ readStreams(); } // implement the method below static void readStreams() { InputStreamReader reader = new InputStreamReader(System.in); // access the stdin values via input stream reader class //Buffered Reader to throw everything inside the output BufferedReader stream = new BufferedReader(reader); String line; // this will be each line feeding from stdin // add the while loop to visit each line while((line = stream.readLine()) != null){ System.out.println(line); } } }
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-read-from-standard-input-in-java
How can we read from standard input in Java?
May 29, 2025 - The System.in is an instance of the InputStream class. It means that all its methods work on bytes, not Strings. To read any data from a keyboard, we can use either the Reader class or the Scanner class.
🌐
UW Computer Sciences
pages.cs.wisc.edu › ~mcw › cs367 › programs › P4 › notes › io.html
I/O in Java
The System class also gives us a stream for reading input from the user. This stream is called System.in and is often referred to as "standard in" (stdin). System.in is an instance of the InputStreamClass.
🌐
Medium
cdbutle.medium.com › java-stdin-and-stdout-a20edf2eedfe
Java Stdin and Stdout. Continuing our journey down the Java… | by Charles Butler Jr | Medium
September 3, 2021 - Continuing our journey down the Java rabbit hole we land on a new principle in the code base. That of reading input. In Java most challenges we’ll face have us reading input from stdin (aka standard input) and somehow manipulating that data to output to stdout.
🌐
Princeton
aofa.cs.princeton.edu › 20recurrence › Java › StdIn.java
StdIn.java
/************************************************************************* * Compilation: javac StdIn.java * Execution: java StdIn * * Reads in data of various types from standard input. * *************************************************************************/ import java.io.BufferedInputStream; import java.util.Locale; import java.util.Scanner; /** * Standard input.
🌐
Reddit
reddit.com › r/learnprogramming › [java]how do i read a continuous input from stdin?
r/learnprogramming on Reddit: [JAVA]How do I read a continuous input from STDIN?
October 20, 2016 -
class Solution{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int N = 0;
		int Q = 0;
		N = scan.nextInt();
		Q = scan.nextInt();
		arr a = new arr(N);
	    ArrayList<Integer> p = new ArrayList<Integer>();
		int A=0 ,B=0 ,X=0 ,Y=0;
		for(int i =0;i<Q;i++){
			A = scan.nextInt(); B = scan.nextInt();
			X = scan.nextInt(); Y = scan.nextInt();
			
			int s = 0;
			for(int j = X;j<=Y;j++){
				s = s +a.f(j,A,B);
			}
			p.add(s);
		}
		for(int i =0;i<Q;i++)
			out.println(p.get(i));
	}
}
class arr{
	val[] v;
	arr(int size){
		v = new val[size];
		Scanner scan = new Scanner(System.in);
		for(int i =0;i<size;i++){
			int q =0, w = 0;
			while((scan.nextInt()) != '\n'){
				 q=scan.nextInt(); w=scan.nextInt();
			}
			  v[i] = new val(q,w);
		}
	}
	int getStart(int i){
		return v[i].getss();
	}
	int getEnd(int i){
		return v[i].getee();
	}
	int f(int i, int a, int b){
		if(a<=getStart(i) && b>=getEnd(i)){
			return 1;
		}
		else
			return 0;
	}
}
class val{
	int s, e;
	val(int start, int end){
		s = start; e = end;
	}
	int getss(){
		return s;
	}
	int getee(){
		return e;
	}
}

Input Data:

10 2
2 10
3 5
4 6
1 7
4 7
8 10
2 6
3 8
5 9
5 6
3 7 2 9
4 7 2 8

If I give these values line-by-line, while typing , gives me the correct result but I need this to be input at once, like a copy-paste onto stdin and then on the final enter key it should show the processed results.

How can I accomplish that?

P.S.: I have done such a thing before but I had a few parameters known in that problem.

🌐
Reddit
reddit.com › r/javahelp › how to read "raw" standard input to a string
r/javahelp on Reddit: How to read "raw" standard input to a String
October 3, 2019 -

Plenty of tutorials and such will teach you how to

Scanner scanner = new Scanner(System.in);

but I can't seem to find a way to just read "all" of standard input to a string. I'm fairly new to Java, and this is probably a very obvious thing to do, but I'm trying to encode raw data that is piped from standard input, and Scanner tokenizes the data (like it's supposed to). Is there any way to just read standard input, as it is, into a String?

🌐
GeeksforGeeks
geeksforgeeks.org › java › ways-to-read-input-from-console-in-java
Ways to Read Input from Console in Java - GeeksforGeeks
January 16, 2026 - We can read input from the user in the command line. Input is buffered, making it faster for large inputs. Suitable for competitive programming and performance-critical applications. Slightly verbose and harder to remember compared to Scanner. ... import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Geeks { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader( new InputStreamReader(System.in)); String s = r.readLine(); System.out.println(s); } }
🌐
GitHub
gist.github.com › 2358345
Java simple read line from stdin. · GitHub
Java simple read line from stdin. GitHub Gist: instantly share code, notes, and snippets.