Joey de Vries Joey de Vries - 4 months ago 19
Java Question

Java increase the value of a leading zero string

I have the issue that my string needs to start with

0000
and every time that code has ran it need to be increase by 1, so after 1 run
0001
etc.

How can I do this?

I have made the String Format with

static String Counter = String.format("%04d", 0);;;


Sorry but for all the people that can't count its basicly everytime some runs it increases with 1.

First Time: 0000 > 0001

Second Time: 0001 > 0002

Thrid Time: 0002 > 0003

Answer

You can use a static variable to hold the value

Edit

After saying correctly what you, all you need is a static variable

public class MyClass {
 private static int counter = 0;

 public static void main(String[] args) {

     for (int x = 0; x < 5; x++) {
         System.out.println(String.format ("%04d", counter++));
     }


  }
}

Output:

00000

00001

00002

00003

00004