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);
}
}
This is a related question on StdIn.
StdInis not a standard java library, as you mentioned that you're following the Princeton course, thatStdIn.javais provided by the course, you probably need to compileStdIn.javaas well, with the commandjavac StdIn.javato generate aStdIn.classfile, this will solve theClass not Found Exception. (the second issue)As to the problem of you writing
java RandomSeq 10 > data.txtin DrJava (which I asked for clarification because I didn't realize DrJava is an IDE, thought it was a book), is because theInteractionssection most likely directly gives the input to your program, meaning it passes whatever you type in there asargs[]for yourmainfunction.The output is printed because you call
system.out.printlnin your code, and DrJava just takes that output and prints it out to the screen, and won't pipe to yourdata.txtfile. I would recommend that since you're using course material that requires stuff like piping to interact with your program in the command line.Lastly
java RandomSeq 10 | java Averageis the same as doingjava RandomSeq 10and then taking that output intojava Average, which the second command is the same as you doingjava Average < data.txt, the error isn't because of how you're piping it, but just because ofStdInnot being present as a compiled.classfile.