The problem with these sites, such as SPOJ and UVA, is that some do not tell you how the input should be done. I've already caught a lot trying to figure out a solution to UVA input problem until, after all, after having to take over some things and also find a code template on one of the sites, I did it.
First you have to assume that it is not a user who is running your program and, rather, a machine.
This machine will grab a txt file with chunky values and run your program with the incoming txt file. It is the same as you run your program through the terminal with the command "java Main < input.txt". You can do this to test your code.
You have to assume, too, that each line of the txt file will output its output at the same instant. That is, in your example, in your program you have to read the number 1 and already generate the output of number 1 on the screen.
Knowing this, and with the proper code, you'll never have input problems on such sites again. Here is the code:
import java.io.IOException;
class Main implements Runnable {
static String ReadLn(int maxLength) {
byte line[] = new byte[maxLength];
int length = 0;
int input = -1;
try {
while (length < maxLength) {
input = System.in.read();
if ((input < 0) || (input == '\n'))
break;
line[length++] += input;
}
if ((input < 0) && (length == 0))
return null;
return new String(line, 0, length);
} catch (IOException e) {
return null;
}
}
public static void main(String args[]) {
Main myWork = new Main();
myWork.run();
}
@Override
public void run() {
new myStuff().run();
}
}
class myStuff implements Runnable {
@Override
public void run() {
// O SEU PROGRAMA AQUI
int input = 0;
String linha;
while((linha = Main.ReadLn(255)) != null && (input = Integer.parseInt(linha)) != 42) {
System.out.println(input);
}
}
}
Notice that here is the program (everything it should do):
class myStuff implements Runnable {
@Override
public void run() {
// O SEU PROGRAMA AQUI
int input = 0;
String linha;
while((linha = Main.ReadLn(255)) != null && (input = Integer.parseInt(linha)) != 42) {
System.out.println(input);
}
}
}
This part says to read to the end of the text file or until you find the number 42:
while((linha = Main.ReadLn(255)) != null && (input = Integer.parseInt(linha)) != 42)
And that's it! I hope it all right! I hope I've helped! Any questions just talk!