Reputation: 21
I'm testing this code in Java with a problem from a programming Judge and I get a "time limit" error. I guess the scanner.hasNextDouble() thingy is not working somehow.
It works with the public inputs shown in the link but it throws this time limit error with the privates.
Any idea to solve this issue?
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
class Main /*AvalPoli2*/ {
public static void main(String[] args) {
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); // dot sep.
nf.setGroupingUsed(false); // don't group in groups of 3
nf.setMaximumFractionDigits(4);
nf.setMinimumFractionDigits(4);
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.US); // read doubles with dot separator
double sum = 0.0000;
double x = 0;
if (scanner.hasNextDouble()) x = scanner.nextDouble();
while (scanner.hasNextDouble()) {
sum *= x;
sum += scanner.nextDouble();
}
System.out.println(nf.format(sum));
}
}
Thank you very much! ^_^
Upvotes: 2
Views: 1039
Reputation: 1
You can create a Reader class by yourself, here is my Reader class code.
/** Reader Class */
static class Reader {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer("");
/** Get next line text */
String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
To use this class, you need to create an object(assume it named reader).
To get a int number, you should reader.nextInt()
.
To get a Double number, you should reader.nextDouble
.
Upvotes: 0
Reputation: 40206
You have to Wrap your System.in, like
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
in = new Scanner(br);
instead,
in = new Scanner(System.in);
Upvotes: 1