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
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
You can not read null from System.in but you can terminate the input on an empty string and then return null if the length of the input is 0.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(getInput());
}
private static String getInput() {
StringBuilder sb = new StringBuilder();
final Scanner scanner = new Scanner(System.in);
for (String line = scanner.nextLine(); !line.isEmpty(); line = scanner.nextLine()) {
sb.append(line).append(System.lineSeparator());
}
return sb.length() == 0 ? null : sb.toString();
}
}
A sample run:
Hello
World
How are you?
Hello
World
How are you?
Another sample run:
null
java - getting input from stdin - Stack Overflow
[JAVA]How do I read a continuous input from STDIN?
How to read "raw" standard input to a String
[JAVA] Why does this code wait for input?
It has to do with the condition.
in.hasNextInt()
It lets you keep looping and then after three iterations 'i' value equals to 4 and testCases[4] throws ArrayIndexOutOfBoundException.
The Solution to do this could be
for (int i = 0; i < testNum; i++) {
*//do something*
}
Update your while to read only desired numbers as below:
while(i < testNum && in.hasNextInt()) {
The additional condition && i < testNum added in while will stop reading the numbers once your have read the numbers equivalent to your array size, otherwise it will go indefininte and you will get ArrayIndexOutOfBoundException when number array testCases is full i.e. you are done reading with testNum numbers.
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.
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?