Read 3 Variable, and DONE.
On the program below. The program run in infinite loop and asked the user for 3 variable forever. I tried to change the while loop to be for loop, but it end up error. How do i make it run once? Like ask for 3 variable then sold the problem that it.
Code :
// Example showing how to pass data from one thread to another
// through a pipe, for the Quadratic Equation Solver.
import java.util.*;
import java.io.*;
public class EquPiping {
public static void main(String args[]) {
try {
PipedOutputStream pout1 = new PipedOutputStream();
PipedInputStream pin1 = new PipedInputStream(pout1);
PipedOutputStream pout2 = new PipedOutputStream();
PipedInputStream pin2 = new PipedInputStream(pout2);
Producer prod = new Producer(pout1);
Filter filt = new Filter(pin1, pout2);
Consumer cons = new Consumer(pin2);
cons.start();
prod.start();
filt.start();
}
catch (IOException e) { }
}
}
////////////////////////////////////////////////////////////////
// This thread reading coefficients from keyboard and passing //
// them through a pipe to the Solver. //
class Producer extends Thread {
private DataOutputStream out;
private Random rand = new Random();
private double num = 0.0;
BufferedReader IOobj = new BufferedReader(new InputStreamReader(System.in));
public Producer(OutputStream os) {
out = new DataOutputStream(os);
}
public void run() {
///////////////////////////////////////////////////////// WAS trying to change the while loop below.
while (true) {
try {
System.out.print("(1) >>>Please enter next coefficient: ");
num = Double.parseDouble(IOobj.readLine());
System.out.println("(1) Passing coefficient into a pipe.");
out.writeDouble(num);
out.flush();
// Short sleeping time may cause output buffering problems.
// sleep(Math.abs(rand.nextInt() % 1000));
sleep(1000);
}
catch (Exception e) {
System.out.println("Error1: " + e);
}
}
}
}
/////////////////////////////////////////////////////////////////
// This thread receiving coefficients from a pipe, solving the //
// equation, and passing roots to the third thread for display.//
class Filter extends Thread {
private DataInputStream in;
private DataOutputStream out;
public Filter(InputStream is, OutputStream os) {
in = new DataInputStream(is);
out = new DataOutputStream(os);
}
public void run() {
for (;;) {
try {
double a = in.readDouble();
double b = in.readDouble();
double c = in.readDouble();
System.out.println("\t(2) >>>Root calculating thread...");
if (b*b-4*a*c >= 0.0) {
double root1 = (-b - Math.sqrt(b*b-4*a*c))/(2.0*a);
double root2 = (-b + Math.sqrt(b*b-4*a*c))/(2.0*a);
out.writeDouble(root1);
out.writeDouble(root2);
}
else {
out.writeDouble(0.0);
out.writeDouble(0.0);
}
System.out.println("\t(2) Root solver passed...");
}
catch (IOException e) {
System.out.println("Error2: " + e);
}
}
}
}
/////////////////////////////////////////////////////////////////
// This thread receiving roots from a pipe and displating them.//
class Consumer extends Thread {
private DataInputStream in;
public Consumer(InputStream is) {
in = new DataInputStream(is);
}
public void run() {
for (;;) {
try {
// Verification that the rootss are actually passed.
double avg1 = in.readDouble();
System.out.println("\t\t(3) >>>Consuming the roots...");
System.out.println("\t\t(3) Displaying root #1: " + avg1);
double avg2 = in.readDouble();
System.out.println("\t\t(3) Displaying root #2: " + avg2);
}
catch (IOException e) {
System.out.println("Error3: " + e);
}
}
}
}
////////////////////////////////////////////////////////////////
I think I found the problem, but will wait if there any opinion.
Re: Read 3 Variable, and DONE.
Why can't you just not use any loops? i.e. code without any loops.
Additionally, why couldn't you simply use a Scanner and ask for 3 user inputs? 5 lines of code.
Re: Read 3 Variable, and DONE.
The standard syntax for a while loop might look something like this:
Code java:
while (someConditionThatIsTrueNowButWillEventuallyBecomeFalse) {
someActionThatMakesAChangeEventuallyCausingTheAboveToBecomeFalse();
{
Your while loop carries this syntax: ...Which would lack one thing. The ability to change the condition from true to false.
There is another way out of a loop, using the keyword break, and that might look something like this:
Code java:
while (true) {
try {
//1) some code that might fail
//2) if NO exception is thrown go to 3a, but if there IS an exception go to 3b
break; //3a) We jump out of the loop, and go to 5)
} catch(exception e) {
//3b)throw, ignore, out.dump, etc
}//4)exiting try/catch, while condition == true, so begin at try block 1)
}//5)
The key thing to see here is that your loop runs forever because that is what you told it to do.
Hopefully you understand why your loop did not work, and how to make them work in the future. Now be sure to read what newbie said in post#2
Re: Read 3 Variable, and DONE.
Quote:
Originally Posted by
newbie
Why can't you just not use any loops? i.e. code without any loops.
Additionally, why couldn't you simply use a Scanner and ask for 3 user inputs? 5 lines of code.
This is part of my teacher template, which he want us to use whatever he already had.