Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Read 3 Variable, and DONE.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    // 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.
    Last edited by daitobu; September 24th, 2012 at 05:43 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Read 3 Variable, and DONE.

    The standard syntax for a while loop might look something like this:
     while (someConditionThatIsTrueNowButWillEventuallyBecomeFalse) {
        someActionThatMakesAChangeEventuallyCausingTheAboveToBecomeFalse();
    {
    Your while loop carries this syntax:
     while (true) {
    //...
    {
    ...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:
     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
    Last edited by jps; September 24th, 2012 at 09:11 PM. Reason: left out a comment in code

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read 3 Variable, and DONE.

    Quote Originally Posted by newbie View Post
    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.

Similar Threads

  1. How to compare variable with multiple variable?
    By mharck in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2012, 09:02 AM
  2. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  3. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM