I want to input a lot doubles
from a text file. This has to be done using piping.
Right now I have this:
public class Average
{
public static void main(String[] args)
{
double sum=0.0;
int cnt = 0;
while (!StdIn.isEmpty())
{
double value=StdIn.readDouble();
sum += value;
cnt++;
}
double average=sum/cnt;
StdOut.println("Average is " + average);
}
}
To execute this I write following command:
$ java Average < data.txt
The problem is that it doesn't work. It asks for more input and then calculates the average of just that input. How do I get it to calculate the average of the values in data.txt?
The issue is that, you are using the built-in Command Prompt
from Dr. Java, which does not support redirections.
You have to use the Command Prompt
program on your PC, and get to your environment according to this procedure(part 4 and 5).