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

    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.

    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);
        }
    }
    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 Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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.

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

  3. #3
    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
    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".

    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");
        }
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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?

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

    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);
    
            }
    Then, I have problem to get this total from both of the for loop above.
    Java Code:
    double total = (sampleData1 + sampleData2);

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

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

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

    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

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