Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2008, 08:47 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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?

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?
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
File Type: txt Input.txt (109 Bytes, 1 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2008, 08:56 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-24-2008, 09:16 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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.
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-24-2008, 09:22 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
Code to do this with:
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; } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-24-2008, 10:20 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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:
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.
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.
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.
Code:
0 0 0 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,
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,
Code:
0 0 0 0 0 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,
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-24-2008, 11:40 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Quote:
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 11:48 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-25-2008, 12:05 AM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-25-2008, 02:21 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
How do you know when you want to insert a 0? Can you test for the case in the loop that outputs input2?
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-25-2008, 05:19 AM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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?
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
Code:
String data = getNextValue(fileData, i); double input1 = parseValue(data); double input2 = 2*input1; //At Line Here, Add three "0"s in front output of input2 double input3 = 5*input2; //At Line Here, Add five "0"s in front output input3 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 05:23 AM.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-25-2008, 05:41 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-25-2008, 08:26 AM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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.
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,
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,
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 08:37 AM.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-25-2008, 03:20 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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".
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-25-2008, 04:54 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
processing in method in worker class
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);//
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-25-2008, 08:06 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
matt_well is on a distinguished road
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.
Code:
List<String> fileData = getData(fileName);
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-26-2008, 06:17 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 780
Nicholas Jordan is on a distinguished road
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:
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 )
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 12:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org