Need help with loops

advertisements

I would like to show the first 200 entries and if I run it again the next 200 hundred and so on. The variable totalResults will be dynamically set later. If I have only 45 Entries it should show those entries, if it has, for example, 540 entries then it should only show the first 200 entries. I use a Properties File. Here is the code:

public class NewClass1 {

public static void main(String args[]) throws FileNotFoundException, IOException {
    int totalResults = 420; //
    int itemsperPage = 10;
    int count = 200;
    int i = 0;

    FileOutputStream output = null;
    Properties prop = new Properties();
    FileInputStream input = null;
    input = new FileInputStream("config.properties");

    // load a properties file
    prop.load(input);

    //get the property value startIndex and print it out
    String iStr = prop.getProperty("i");
    int startIndex = Integer.parseInt(iStr);
    System.out.println("startIndex " + startIndex);

    //get the property value startIndex and print it out
    String iCount = prop.getProperty("count");
    int intCount = Integer.parseInt(iCount);
    System.out.println("intCount " + intCount);

    count = intCount;

    for (i = startIndex; i <= (count / itemsperPage); i++) {
        //System.out.println("count/itemsperPage "+(count / itemsperPage));
        System.out.println("count for " + count);
        System.out.println("i for  " + i);

        if (totalResults >= count) {
            System.out.println(" i " + i);
            System.out.println("last");
            output = new FileOutputStream("config.properties");
            //prop.setProperty("pos", strI+"0");
            String strI = "" + (i);
            prop.setProperty("i", strI);

            String strCount = "" + (count + 200);
            prop.setProperty("count", strCount);

            prop.store(output, null);

        }else{

            System.out.println("else");
            break;
        }

    }

  }

}

If I run this program the first time, the config.properties key and value should be declared for count=200; and i=1;

The output on the first run is:

  • i for 1, i for 2, i......, i for 20

and if I run it again:

  • i for 20, i for 21,i....., i for 40

It shows the starting index up to 40 but there are 420 entries. Shouldn't it print out the starting index up to 420? Does anybody know what's wrong?


I get that you've already chosen an accepted answer. But if you need the solution provided to you. I've added the source code below that will achieve your intended operation.

And yes as Floegipoky has pointed out your code is not easily readable. Just make sure that the relevant code is grouped together -- that solves half your readability issues.

import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFile
{
        public static void main(String args[]) throws FileNotFoundException, IOException
        {
                Properties config = new Properties();
                config.load(new FileInputStream("config.properties"));

                int itemsPerPage = Integer.parseInt(config.getProperty("itemsPerPage"));
                int currentIndex = Integer.parseInt(config.getProperty("currentIndex"));
                int totalItemCount = Integer.parseInt(config.getProperty("totalItemCount"));

                if(currentIndex < totalItemCount)
                {
                        for(int iterator = 0; iterator < itemsPerPage; iterator++)
                        {
                                if(currentIndex < totalItemCount)
                                {
                                        //TODO Insert what ever processing of those batch operations here
                                        currentIndex++;
                                        continue;
                                }
                        }

                        config.setProperty("currentIndex", ((Integer)currentIndex).toString());

                        config.store(new FileOutputStream("config.properties"),null);
                }
        }
}