Results 1 to 7 of 7
- 08-02-2008, 06:01 PM #1
Problem to use different for loop to add up
Below I create a program that can run, but I am dealing with the for loop for different 'i' starting value.
I am having problem to use different for loop to add up.Java Code:import java.io.*; import java.util.*; public class Addup { public static void main(String[] args) throws java.io.IOException { String fileName = "Values.txt"; // Read files and collect data for processing: List<String> fileData = getData(fileName); List<String> process1 = getData(fileName); List<String> process2 = getData(fileName); String path = "C:\\Total.txt"; PrintWriter pw = new PrintWriter( new FileOutputStream(path)); int maxSize = fileData.size(); for(int i = 0; i < maxSize; i++) { String data1 = getNextValue(process1, i); String data2 = getNextValue(process2, i); double sampleData1 = parseValue(data1); double sampleData2 = parseValue(data2); double total = (sampleData1 + sampleData2); pw.printf("%1.4f%n", total); } pw.close(); System.out.print( "Total.txt now at C drive"); } 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) return ""; return list.get(index); } private static double parseValue(String s) { if(s.equals("")) return 0; return Double.parseDouble(s); } }
How to make the program so that the sampleData1 starts to read from 1 and sampleData2 starts to read from 2, from Values.txt text file before sum them up?
Here I mean the for(int i = 0; i < maxSize; i++), I would like to change the 'i = 0' to 'i = 1' and 'i = 2' for different for loop.
Any guides are appreciated. Thanks.
- 08-02-2008, 07:54 PM #2
Are those filenames? Are you saying that you want to read from one file starting at line 1 and from the other file from line 2?sampleData1 starts to read from 1 and sampleData2 starts to read from 2,
Since most files are sequential, you must read and skip the records that you do not want to process.
Does that mean to sum all the elements of something?to add up
Yes you can have a for loop that starts the index at any value you want: for(int i=77; i < total; i++)
- 08-02-2008, 08:58 PM #3
It read only from one file "Values.txt".
The code I created have only one for loop.Java Code:for(int i = 0; i < maxSize; i++) { String data1 = getNextValue(process1, i); String data2 = getNextValue(process2, i); double sampleData1 = parseValue(data1); double sampleData2 = parseValue(data2); double total = (sampleData1 + sampleData2); pw.printf("%1.4f%n", total); } pw.close(); System.out.print( "Total.txt now at C drive"); }
I mean that how to seperate into two for loop as I want sampleData1 starts to read from 1 and sampleData2 starts to read from 2.
Yes, sum them each row by row.
- 08-02-2008, 09:42 PM #4
What does "from 1" mean if there is only one file to read from?I want sampleData1 starts to read from 1 and sampleData2 starts to read from 2.
Do you mean to get the first number into one variable and the next number into the second variable and the next number into the first variable and the next number into the second variable, etc ?
What is the program supposed to do? What is written into the output file?
- 08-03-2008, 08:06 AM #5
I mean the for loop like this,
Then, I have problem to get this total from both of the for loop above.Java Code:for(int i = 1; i < maxSize; i++) { String data1 = getNextValue(process1, i); double sampleData1 = parseValue(data1); } for(int i = 2; i < maxSize; i++) { String data2 = getNextValue(process2, i); double sampleData2 = parseValue(data2); }
Java Code:double total = (sampleData1 + sampleData2);
- 08-03-2008, 01:51 PM #6
Can you sum up sampleData1 and then sampleData2 and add the results together?
In the first loop:
totalSampD1 += sampleData1;
and in the second loop:
totalSampD2 += sampleData2;
Then
total = totalSampD1 + totalSampD2;
Can you write out description of what the program is to do in simple steps? where each step does one simple thing. From beginning to end of the processing to be done. List each step on a separate line.What is the program supposed to do? What is written into the output file?
For example here are the steps you take when walking:
Shift weight to right foot.
Lift left foot
move left foot forward
put left foot on the ground
shift weight to left foot
lift right foot off the ground
move right foot forward
put right foot on the ground
Go back to step one and repeatLast edited by Norm; 08-03-2008 at 04:18 PM.
- 08-03-2008, 10:24 PM #7
Hi, Norm. LOL. Nevermind. I have done it by modifying i to i+1 and i+2.
Java Code:for(int i = 0; i < maxSize; i++) { String data1 = getNextValue(process1, i+1); String data2 = getNextValue(process2, i+2); double sampleData1 = parseValue(data1); double sampleData2 = parseValue(data2); double total = (sampleData1 + sampleData2); pw.printf("%1.4f%n", total); }
Similar Threads
-
while loop
By Unknown1369 in forum New To JavaReplies: 5Last Post: 07-08-2008, 10:15 AM -
Beginner's Problem on Loop/If statement
By obdi in forum New To JavaReplies: 2Last Post: 07-07-2008, 01:41 AM -
For loop problem
By mcal in forum New To JavaReplies: 32Last Post: 01-25-2008, 03:51 PM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM -
eternal loop problem
By sandor in forum New To JavaReplies: 3Last Post: 04-29-2007, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks