I have a class RandomSeq which prints "args[0]" random doubles and a class Average which prints the average of StdIn. I'm using DrJava for writing codes and compiling.I've downloaded the StdIn and out libraries and put them in the same folder of my classes. I'm learning Java.
First problem is in the "Interactions" section of DrJava. When I write java RandomSeq 10 > data.txt
instead of creating the text file, it prints the output. Why?
Then I typed the same command in Windows command line. It created the txt file successfully.
Now I want to pipe the output of RandomSeq 10 to the input of Average. Writing java RandomSeq 10 | java Average
in DrJava's Interactions section causes funny behaviour. Writing it in cmd prints the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: StdIn
at Average.main(Average.java:7)
Caused by: java.lang.ClassNotFoundException: StdIn
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Even java Average < data.txt
shows the same error.Why?
public class RandomSeq
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
for (int i = 0; i < N; i++)
{
System.out.println(Math.random());
}
}
}
public class Average
{
public static void main(String [] args)
{
double S = 0;
int i = 0;
while (!StdIn.isEmpty())
{
double n = StdIn.readDouble();
S += n;
i++;
}
S = S/i;
//StdOut.printf("The mean is %.3f", S);
StdOut.println(S);
}
}