Results 1 to 15 of 15
  1. #1
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Smile Delay on inputs during calculation

    Hi, dealing with a program and there's only one input file name "Input.txt"
    Just one input and branch it to get different inputs.

    Below is the program created that can run, but I would like to add something to play with it. I would like to know how to delay the input during calculation?

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class ReadTextFilesRx
    {
        public static void main(String[] args) throws java.io.IOException
        {
            String fileName = "Input.txt";
    
            // Read files and collect data for processing:
            List<String> fileData = getData(fileName);
    
            // Let's see what we have in the lists:
            System.out.println("file1Data = " + fileData);
    
            // Now it's easy to get the number of data items:
            System.out.println("file1Data size = " + fileData.size());
    
            //After that you can calculate them and write to file.
            String path = "C:\\readTextFilesResults.txt";
            PrintWriter pw = new PrintWriter(
                             new FileOutputStream(path));
            pw.printf("%s    %s      %s      %s       %s%n",
                      "Index", "Input1", "Input2", "Input3", "Input4");
            int maxSize = fileData.size();
            for(int i = 0; i < maxSize; i++)
            {
                String data = getNextValue(fileData, i);
                double input1 = parseValue(data);
                double input2 = 2*input1;
                /*How to delay three steps by adding three "0"s in front input2 
                before continue multiply by 5 occur?*/
                
                double input3 = 5*input2;
                /*How to delay five steps by adding five "0"s in front input3 
                before continue addition of 1.2 occur?*/
                
                double input4 = input3 + 1.2;
                //after that i plan to sum up all inputs.
    
                pw.printf("%3s %10s %12.2f %12.2f %12.2f%n",
                           i, input1, input2, input3, input4);
            }
            pw.close();
    
        }
    
        private static List<String> getData(String filePath) throws IOException
        {
            List<String> list = new ArrayList<String>();
            FileReader fr = new FileReader(filePath);
            BufferedReader br = new BufferedReader(fr);
            String buffer;
            while( (buffer = br.readLine()) != null)
            {
                list.add(buffer);
            }
            br.close();
            return list;
        }
    
        private static String getNextValue(List<String> list, int index)
        {
            if(index > list.size()-1) // gone past end of list
                return "";
            return list.get(index);
        }
    
        private static double parseValue(String s)
        {
            if(s.equals("")) // missing data values appear as empty string
                return 0;
            return Double.parseDouble(s);
        }
    }



    The questions I place on the comments,
    How to delay three steps by adding three "0"s in front input2 before continue multiply by 5 occur?
    How to delay five steps by adding five "0"s in front input3 before continue addition of 1.2 occur?
    Java Code:
            PrintWriter pw = new PrintWriter(
                             new FileOutputStream(path));
            pw.printf("%s    %s      %s      %s       %s%n",
                      "Index", "Input1", "Input2", "Input3", "Input4");
            int maxSize = fileData.size();
            for(int i = 0; i < maxSize; i++)
            {
                String data = getNextValue(fileData, i);
                double input1 = parseValue(data);
                double input2 = 2*input1;
                /*How to delay three steps by adding three "0"s in front input2 
                before continue multiply by 5 occur?*/
                
                double input3 = 5*input2;
                /*How to delay five steps by adding five "0"s in front input3 
                before continue addition of 1.2 occur?*/
                
                double input4 = input3 + 1.2;
                //after that i plan to sum up all inputs.
    
                pw.printf("%3s %10s %12.2f %12.2f %12.2f%n",
                           i, input1, input2, input3, input4);
            }
            pw.close();
    How to create a method something likes to send the value outside the for loop to do the delay and then send back again for further calculation?
    Attached Files Attached Files

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Timer task

    Use java.util.Timer to schedule a task to execute once 5 seconds have passed : Timer**Development Class**Java

    Whatever that code does, replace it with print( index++ ); You will have to either store your reads in a Collection, Array or Enumeration - then pull them by index incremeted on each timer event.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    how to delay the input during calculation
    Not sure what this means?
    Are you asking how to pause in reading an input file while you do something else? In other words, read a line, do something, read a line, do something, etc

    That's sort of a natural way that programs work. You read when you want and you process what was read and so on.
    How to delay three steps
    What is a step? Is that a unit of time or what?

    Perhaps you could explain by an example showing what it currently does and what you want it to do. For example:

    The program currently does this:
    adds 1 to total
    multiples total by 4
    reads another line

    I want it to do this:
    add 1 to total
    add total to accum <<<<<< insert this step
    multiples total by 4
    reads another line

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post Code to do this with:

    Java Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimedTask extends TimerTask
    {
        double[] SomeNumbers = { 01.200 ,  00.100 ,  02.500 ,  04.600 ,  90.100 ,  10.300 ,  12.600 ,  02.200 ,  7.1000 ,  4.5100 ,  4.6000 ,  19.100 ,  10.300 ,  00.000 ,  01.220 ,  -2.100 ,  09.500 ,  12.000 ,  43.000 ,  77.100 ,  00.500 };
        public void run()
        {
            int lenghtOfStuff = SomeNumbers.length;// Nifty, huh.
            do
            {
                System.out.println((new Double(SomeNumbers[--lenghtOfStuff])).toString());
            }
            while( lenghtOfStuff > 0x00000000);//
        }
        /**
        * See saharan gourds. ground-creeping, inedible, 
        * wild gourds, the size of small melons 
        * at: Wildlife in Libyan Sahara desert - Africa
        * http://www.temehu.com/Wild-life-in-sahara.htm
        *
        */
        public static void main(String[] args)
        {
            // So main can return, program exits after reasonable delay.
            Timer thisTime = new Timer(true) ;
    
            // Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
    
            thisTime.schedule(new TimedTask(),0x00000000,5000);// Delay is in milliseconds.
            return;
        }
    }

  5. #5
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not sure what this means?
    Are you asking how to pause in reading an input file while you do something else? In other words, read a line, do something, read a line, do something, etc

    That's sort of a natural way that programs work. You read when you want and you process what was read and so on.

    What is a step? Is that a unit of time or what?

    Perhaps you could explain by an example showing what it currently does and what you want it to do. For example:

    The program currently does this:
    adds 1 to total
    multiples total by 4
    reads another line

    I want it to do this:
    add 1 to total
    add total to accum <<<<<< insert this step
    multiples total by 4
    reads another line


    It's something like this:
    Java Code:
                String data = getNextValue(fileData, i);
                double input1 = parseValue(data);
                double input2 = 2*input1;
                /*How to delay three steps by adding three "0"s in front input2 
                before continue multiply by 5 occur?*/
                
                double input3 = 5*input2;
                /*How to delay five steps by adding five "0"s in front input3 
                before continue addition of 1.2 occur?*/
                
                double input4 = input3 + 1.2;


    The program currently receives data from "Input.txt" file as input1 as below.
    Java Code:
    1.3
    9.1
    2.9
    0.6
    9.1
    10.8
    17.6
    22.2
    7.11
    3.51
    0.6
    9.1
    0.3
    0.01
    1.22
    -2.1
    9.51
    2
    4.3
    7.1


    Then, multiply input1 by 2 to get input2 as below.
    Java Code:
    2.40
    0.20
    5.00
    9.20
    18.20
    20.60
    25.20
    4.40
    14.20
    9.02
    9.20
    38.20
    20.60
    0.00
    2.44
    -4.20
    19.00
    24.00
    86.00
    154.20
    1.00


    After that this is what I mean to do, to delay three steps by adding three "0"s in front input2 before the continue multiply by 5 occur to get input3 as below.
    Java Code:
    [B][COLOR="Red"]0
    0
    0[/COLOR][/B]
    2.40
    0.20
    5.00
    9.20
    18.20
    20.60
    25.20
    4.40
    14.20
    9.02
    9.20
    38.20
    20.60
    0.00
    2.44
    -4.20
    19.00
    24.00
    86.00
    154.20
    1.00


    Now the multiply of 5 is done for the delayed data obtained above, to get the result input3 as below,
    Java Code:
    0
    0
    0
    12.00
    1.00
    25.00
    46.00
    91.00
    103.00
    126.00
    22.00
    71.00
    45.10
    46.00
    191.00
    103.00
    0.00
    12.20
    -21.00
    95.00
    120.00
    430.00
    771.00
    5.00


    And here same thing again what I need to do, to delay five steps by adding five "0"s in front input3 before the continue addition of 1.2 occur as below,
    Java Code:
    [B][COLOR="Red"]0
    0
    0
    0
    0[/COLOR][/B]
    0
    0
    0
    12.00
    1.00
    25.00
    46.00
    91.00
    103.00
    126.00
    22.00
    71.00
    45.10
    46.00
    191.00
    103.00
    0.00
    12.20
    -21.00
    95.00
    120.00
    430.00
    771.00
    5.00


    Finally the addition of 1.2 is done for the delayed data obtained above, to get the result input4 as below,
    Java Code:
    1.2
    1.2
    1.2
    1.2
    1.2
    1.2
    1.2
    1.2
    13.20
    2.20
    26.20
    47.20
    92.20
    104.20
    127.20
    23.20
    72.20
    46.30
    47.20
    192.20
    104.20
    1.20
    13.40
    -19.80
    96.20
    121.20
    431.20
    772.20
    6.20

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Have no idea what all your posts of numbers means?
    There is no need to post more than 2 or 3 numbers with comments describing the before and after.
    Can you explain in english what you want to change?

    input1, input2 etc are variables of type double.
    input1 appears to be a value that comes from a method call (perhaps read in)
    input2 is the results of a computation, it is NOT read in. Naming it input2 seems confusing to me. How about calling it: input1Times2.
    The same for input3 and input4.
    by adding three "0"s in front input2
    This must mean that somewhere you are outputing the contents of input2 and you want to insert the 000s into that output somewhere.
    Last edited by Norm; 07-24-2008 at 09:48 PM.

  7. #7
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have no idea what all your posts of numbers means?
    There is no need to post more than 2 or 3 numbers with comments describing the before and after.
    Can you explain in english what you want to change?

    input1, input2 etc are variables of type double.
    input1 appears to be a value that comes from a method call (perhaps read in)
    input2 is the results of a computation, it is NOT read in. Naming it input2 seems confusing to me. How about calling it: input1Times2.
    The same for input3 and input4.

    This must mean that somewhere you are outputing the contents of input2 and you want to insert the 000s into that output somewhere.
    Hi Norm, sorry for my long explaination as I may not good in explaining the code.
    Yes, in simple, mean that somewhere outputing the contents of input2 and want to insert the 000s into that output. How to do it in this case?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    How do you know when you want to insert a 0? Can you test for the case in the loop that outputs input2?
    Java Code:
    If the code was:
      start loop:
       compute input2
       output input2
      end loop
    
    Change the code to:
      start loop:
        if(test if to insert 0)   <<<<< add this test
          insert 0                  <<<< add code to output a 0
       compute input2
       output input2
      end loop

  9. #9
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    How do you know when you want to insert a 0? Can you test for the case in the loop that outputs input2?
    Java Code:
    If the code was:
      start loop:
       compute input2
       output input2
      end loop
    
    Change the code to:
      start loop:
        if(test if to insert 0)   <<<<< add this test
          insert 0                  <<<< add code to output a 0
       compute input2
       output input2
      end loop
    Java Code:
                String data = getNextValue(fileData, i);
                double input1 = parseValue(data);
    
                double input2 = 2*input1;
    
                [COLOR="Red"]//At Line Here, Add three "0"s in front output of input2[/COLOR]
                
                double input3 = 5*input2;
    
                [COLOR="Red"]//At Line Here, Add five "0"s in front output input3 [/COLOR]
                
                double input4 = input3 + 1.2;
    I am not sure how to create this for loop but something like above, it is just a method to delay the outputs for further inputs.
    The code above is inside a for loop, that I plan not to create a for loop inside a for loop again. Can the for loop be created inside another "method"?
    Can you demonstrate just like, how to send the outputs outside that for loop to a "method" to process and send back.
    Last edited by matt_well; 07-25-2008 at 03:23 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    The code you show does NOT show input2 or input3 being output.

    Why do you have 3 variables with names starting with input?

    Sorry, I don't know how to help you. I can't understand what you are trying to do. The last code you posted shows several variables being assigned values but it doesn't show anything being output.
    Maybe hardwired will come along and write you a new program.

  11. #11
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The code you show does NOT show input2 or input3 being output.

    Why do you have 3 variables with names starting with input?

    Sorry, I don't know how to help you. I can't understand what you are trying to do. The last code you posted shows several variables being assigned values but it doesn't show anything being output.
    Maybe hardwired will come along and write you a new program.
    The input2 is the branch from input1, which mean input2 reuse the input1 for further calculation and so on.

    Program can run but output all null
    Previously hardwired got teach me using this kind of code which is to add the extra values to the list after reading the file, but it that case occured at the input reading there where the input is still a String.
    Java Code:
    // Add 3 values to beginning of file2Data.
    for(int i = 0; i < 3; i++) {
        file2Data.add(0, "0");
    }

    For example, I add three "0"s to delay my input1 by adding this code to program at the top of this post,
    Java Code:
                for(int i = 0; i < 3; i++) {
    			    fileData.add(0, "0");
    			    }
            // Let's see what we have in the lists:
            System.out.println("file1Data = " + fileData);
    The output will get delayed by adding three "0"s in front and become,
    Java Code:
    Index    Input1      Input2      Input3       Input4
      0        0.0         0.00         0.00         1.20
      1        0.0         0.00         0.00         1.20
      2        0.0         0.00         0.00         1.20
      3        1.2         2.40        12.00        13.20
      4        0.1         0.20         1.00         2.20
      5        2.5         5.00        25.00        26.20
      6        4.6         9.20        46.00        47.20
      7        9.1        18.20        91.00        92.20
      8       10.3        20.60       103.00       104.20
      9       12.6        25.20       126.00       127.20
     10        2.2         4.40        22.00        23.20
     11        7.1        14.20        71.00        72.20
     12       4.51         9.02        45.10        46.30
     13        4.6         9.20        46.00        47.20
     14       19.1        38.20       191.00       192.20
     15       10.3        20.60       103.00       104.20
     16        0.0         0.00         0.00         1.20
     17       1.22         2.44        12.20        13.40
     18       -2.1        -4.20       -21.00       -19.80
     19        9.5        19.00        95.00        96.20
     20       12.0        24.00       120.00       121.20
     21       43.0        86.00       430.00       431.20
     22       77.1       154.20       771.00       772.20
     23        0.5         1.00         5.00         6.20

    What I am doing now is how to add three "0"s in front output of input2 using the program at the top of this post?
    Last edited by matt_well; 07-25-2008 at 06:37 AM.

  12. #12
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    It is to add three "0"s in front output of input2, how to do it as for(int i = 0; i < 3; i++)? where "i" cannot equals to "-3".

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default processing in method in worker class

    Java Code:
    /* 
     * This code is a discussion of an opinion in a technical 
     * forum and has no legitimate derivative use.
     *
     * Demonstrate for loop be created inside another  "method"? 
     *
     * Demonstrate how to send the outputs outside
     * that for loop to a "method" to process and send back.
     */ 
     public class Processor
     {
         public process(List values)
         {
             .....// code to process omitted.
         }
     }
     
     class ReadTextFilesRx
     {
         List<String> fileData // as you have it.
         Processor processor = new Processor();//
         processor.process(fileData);//
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  14. #14
    matt_well's Avatar
    matt_well is offline Member
    Join Date
    Jul 2008
    Posts
    59
    Rep Power
    0

    Default

    Hi, wanna ask is there anything like the List<String> to be in double format, like List<Double>? As I am dealing with Double variable inside the for loop, not String.
    Java Code:
    List<String> fileData = getData(fileName);

  15. #15
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default yes, list of Doubles is possible

    Quote Originally Posted by matt_well View Post
    Hi, wanna ask is there anything like the List<String> to be in double format, like List<Double>? As I am dealing with Double variable inside the for loop, not String.
    Yes:
    Java Code:
    String linebuffer;
    List<Double> fileData = new List<Double>(); 
    
    // vastly abbreviated ....
    
    while ((linebuffer=fileName.readline())!=null)fileData.add(Double.parseDouble(linebuffer));
    ( neutral tone ->) Any questions? (<- neutral tone )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 10:35 PM
  2. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 06:37 AM
  3. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM
  4. Java program that stores user inputs
    By staticy2003 in forum Advanced Java
    Replies: 6
    Last Post: 01-24-2008, 07:46 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •