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

07-24-2008, 08:47 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
|
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?
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?
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?
|
|

07-24-2008, 08:56 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
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
|
|

07-24-2008, 09:16 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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.
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
|
|

07-24-2008, 09:22 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
Code to do this with:
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;
}
}
|
|

07-24-2008, 10:20 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Originally Posted by Norm
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:
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.
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.
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.
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,
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,
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,
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
|
|

07-24-2008, 11:40 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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 11:48 PM.
|
|

07-25-2008, 12:05 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Originally Posted by Norm
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?
|
|

07-25-2008, 02:21 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
How do you know when you want to insert a 0? Can you test for the case in the loop that outputs input2?
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
|
|

07-25-2008, 05:19 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Originally Posted by Norm
How do you know when you want to insert a 0? Can you test for the case in the loop that outputs input2?
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
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.
|
|

07-25-2008, 05:41 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

07-25-2008, 08:26 AM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
Originally Posted by Norm
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.
// 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,
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,
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.
|
|

07-25-2008, 03:20 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
|
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".
|
|

07-25-2008, 04:54 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
processing in method in worker class
/*
* 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
|
|

07-25-2008, 08:06 PM
|
 |
Member
|
|
Join Date: Jul 2008
Posts: 59
|
|
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.
List<String> fileData = getData(fileName);
|
|

07-26-2008, 06:17 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 780
|
|
|
yes, list of Doubles is possible
Originally Posted by matt_well
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:
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|