Cli clip = getClipFromSomewhere();
// Get the gain control from clip
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// set the gain (between 0.0 and 1.0)
double gain = 0.25;
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
written by objects
\\ tags: audio, Clip, Player, sound, volume
Java introduced scripting support with Java 6.
Heres a simple example showing how to use the JavaScript scripting engine to evaluate an expression.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Object result = engine.eval("6 * (3 + 5)");
written by objects
\\ tags: evaluate, expression, javascript, script, scripting
We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used.
It reads the input from the user and checks if it is a valid double. If its not it prompts the user again. Could easily be modified for different validation that you may require.
Scanner input = new Scanner( System.in );
Double value = null;
while (value==null) {
System.out.print("Please enter number: ");
String s = input.nextLine();
if (s.length()==0) {
System.out.println("You did not enter a value; Try again");
} else {
try {
value = new Double(s);
} catch (Exception ex) {
System.out.println("You did not enter a valid value; Try again");
}
}
}
double d = value.doubleValue();
written by objects
\\ tags: console, input, prompt, stdin, validate
Recent Comments