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

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);
    }
}
I am having problem to use different for loop to add up.

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.
Attached Files:
File Type: txt Values.txt (86 Bytes, 0 views)
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-02-2008, 08:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
sampleData1 starts to read from 1 and sampleData2 starts to read from 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?
Since most files are sequential, you must read and skip the records that you do not want to process.

Quote:
to add up
Does that mean to sum all the elements of something?

Yes you can have a for loop that starts the index at any value you want: for(int i=77; i < total; i++)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-02-2008, 09:58 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
Rep Power: 0
matt_well is on a distinguished road
Default
Originally Posted by Norm View Post
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?
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?

Yes you can have a for loop that starts the index at any value you want: for(int i=77; i < total; i++)
It read only from one file "Values.txt".

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");
    }
The code I created have only one for loop.
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-02-2008, 10:42 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
I want sampleData1 starts to read from 1 and sampleData2 starts to read from 2.
What does "from 1" mean if there is only one file to read from?
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-03-2008, 09:06 AM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
Rep Power: 0
matt_well is on a distinguished road
Default
Originally Posted by Norm View Post
What does "from 1" mean if there is only one file to read from?
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?
I mean the for loop like this,

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);

        }
Then, I have problem to get this total from both of the for loop above.
Code:
double total = (sampleData1 + sampleData2);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-03-2008, 02:51 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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;
Quote:
What is the program supposed to do? What is written into the output file?
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.
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 repeat

Last edited by Norm; 08-03-2008 at 05:18 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-03-2008, 11:24 PM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
Rep Power: 0
matt_well is on a distinguished road
Default
Originally Posted by Norm View Post
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.
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 repeat
Hi, Norm. LOL. Nevermind. I have done it by modifying i to i+1 and i+2.

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);
        }
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
while loop Unknown1369 New To Java 5 07-08-2008 11:15 AM
Beginner's Problem on Loop/If statement obdi New To Java 2 07-07-2008 02:41 AM
For loop problem mcal New To Java 32 01-25-2008 04:51 PM
can you help me with this for loop? java_fun2007 New To Java 6 12-22-2007 11:20 AM
eternal loop problem sandor New To Java 3 04-29-2007 04:55 PM


All times are GMT +2. The time now is 12:37 AM.



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