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);
}
}
StdIn is not from a built-in Java library. It is part of a 3rd party library that is contained in a stdlib.jar file that can be downloaded from here.
The path to that JAR file must be in the -CLASSPATH option of your command-line javac and java invocations. The Using the standard libraries section of this page shows you how to do this; the -cp option is just shorthand for -CLASSPATH. It is important to learn how to put libraries onto your CLASSPATH, as you'll eventually be doing it all the time with Java.
Your Average.java code must also import the StdIn class. Since StdIn does not reside in a named package, you just say import StdIn; at the top of your code.
Alternatively, you could just use System.in, rather than this library. You probably also want a higher level stream construct around that. Here's a nice example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With