Rendy
Rendy

Reputation: 101

Java arrays print next element each time executed

Let's say I have this array

int[] anArray = { 100, 200, 300};

Now, I want everytime my program execute it prints the next element in order. For example, first time I execute java myProgram, the program print the first element "100". For second time I execute the program, it will print the second element "200". and so on. But the forth time, it will repeat itself. and print the first element again.

I imagine, I could save the last element that I print. But I didn't know how. Any ideas?

Upvotes: 0

Views: 1450

Answers (7)

apines
apines

Reputation: 1262

As people here suggested, you should save in an external file the current index, and reset it when reaching the last index. Here is an example code that might help you:

private static final String FILE_NAME = "indexFile";
private static int[] anArray = { 100, 200, 300};

public static void main(String args[]) throws IOException {
    System.out.println(anArray[getIndex()]);

}

private static Integer getIndex() throws IOException {
    BufferedReader in = new BufferedReader(new FileReader(FILE_NAME));
    int index = Integer.parseInt(in.readLine());
    in.close();

    Integer newIndex = (index >= anArray.length - 1) ? 0 : index + 1;
    Writer out = new OutputStreamWriter(new FileOutputStream(FILE_NAME));
    out.write(newIndex.toString());

    out.close();
    return index;
}

When you write your code, you should add validation that the file exists etc.

Upvotes: 2

BalusC
BalusC

Reputation: 1108742

You can use java.util.prefs.Preferences to store application-specific preferences.

Here's an SSCCE:

package com.stackoverflow.q8915076;

import java.util.prefs.Preferences;

public class Test {

    public static void main(String[] args) throws Exception {
        Preferences preferences = Preferences.userNodeForPackage(Test.class);
        int executeCount = preferences.getInt("execute_count", 0);
        executeCount++;
        System.out.println(executeCount);
        preferences.putInt("execute_count", executeCount);
    }

}

The output increments each time with 1 when you run the application. In the above example, the value is dependent on the currently logged-in user. You can use Preferences#systemNodeForPackage() instead if you want it to be system-wide.

In Windows machines, it's stored in the Windows registry.

See also:

Upvotes: 7

Cratylus
Cratylus

Reputation: 54074

Programs that need to store information each time they start, they persist them in HD.

A typical example is storing the user's preferences.

I would suggest to store the index in a file (prefixed by "." show that is is a hidden file) and read the index from that file on startup.

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

You can save the index in a file. When the index is equal to array.length - 1 reset the index.

Upvotes: 0

Divya
Divya

Reputation: 2622

You'll have to save an index of the "current" element in the array into a file. Then read the index from the file, print the element, increase the index and update it in the file.

Upvotes: 2

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

Well since you need it between different executions you need to persist a counter somewhere. Most common would be either a file or in a database (depending on the size of the project a DB could be an overkill).

If you are using Java 7 then working with files is really a breeze. Not that it was hard with Java 6 or anything. You should check the NIO or NIO2 (for Java7).

Upvotes: 0

NPE
NPE

Reputation: 500367

The easiest way is store the index of the last-printed element in a file.

You'll need to handle the case where the file doesn't exist yet (the very first run), and also the case where the index goes past the end of array and has to wrap around.

Upvotes: 0

Related Questions