The problem with these sites, such as SPOJ and UVA, is that some do not say how the input should be done. I’ve caught up a lot trying to figure out a solution to input problem in UVA until, at last, after having to take over some things and, also, find a code template on one of the sites, I got.
First you have to assume that it is not a user who is running your program and, yes, a machine.
This machine will take a txt file with abirtrários values and run your program with the input txt file. It is the same as running your program from the terminal with the command "java Main < input.txt". You can do this to test your code.
You have to assume, too, that at each line of the txt file your output will have to be generated 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 the number 1 on the screen.
Knowing this and, with the proper code, you will never again have input problems on sites of this type. Follow 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 works out! I hope I’ve helped! Any questions just talk!
Take advantage of what you did in ideone and put the link there.
– Maniero
It was bad hehe, here the link: http://ideone.com/0DyF7F
– Vinícius Novelli
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero