Meowminator
Meowminator

Reputation: 71

How to stop an integer from reseting to original value with each run?

I've written an automated selenium jar that will change my Discord avatar (https://discordapp.com/). I have a folder where all images are stored that are called by Selenium to be uploaded. I've also written an automater that will change the names of the images so the next one will be uploaded when the jar is launched.

I want to make the process easier by declaring an integer that will increase with each run, which will be then turned to a string to the corresponding image name (i.e. "avatar_1", "avatar_2", "avatar_3").

But every time I run it, the integer resets to the original value. Is there a way for the jar to remember the previous value of the integer with every run?

Here is what I came up with:

static WebDriver driver = new ChromeDriver();
static WebDriverWait wait = new WebDriverWait(driver, 10);
public static int i = 0;

public static void main (String[] args) throws InterruptedException {
    changeAvatar();
}

public static void changeAvatar() throws InterruptedException {

    driver.navigate().to("https://discordapp.com/");

    changeAvatarSteps.loginScreen();
    changeAvatarSteps.enterInformation();
    changeAvatarSteps.loginToAccount();
    changeAvatarSteps.userSettings();
    changeAvatarSteps.editAccount();
    changeAvatarSteps.changeAvatar(); //pathToAvatar+Integer.toString(i);

    driver.quit();

    i++;

}

Upvotes: 0

Views: 144

Answers (1)

Nick Chakraborty
Nick Chakraborty

Reputation: 26

The keyword static only makes it so the variable does not reset every time you create a new object with that variable as a parameter. Here you are not dealing with objects, so static would not help you.

The way to solve this problem would be, as @Matt said, to store the integer value in a separate file and increment it as necessary across different executions of your program. Some classes to look into for file input/output functions are FileInputStream and FileOutputStream. A great resource for those classes is below.

https://www.tutorialspoint.com/java/java_files_io.htm

Hope this helped!

Upvotes: 1

Related Questions