0

Here I have written the logic for generating customer id but how can I preserve the value of i so that when next time function is called i=11 instead of i=10. Whenever I am running this it is starting from i=10 everytime.

public class TestId {

    public static void main(String[] args) {
        String start = "CUST";
        String total = "";
        int i = 10;
        if (10 <= i && i < 100) {
            total = start + "000" + i;
            i++;
        } else {

            if (i <= 100 && i < 1000) {
                total = start + "00" + i;
                i++;
            } else {
                if (i <= 1000 && i < 10000) {
                    total = start + "0" + i;
                    i++;
                } else {

                    total = start + i;
                    i++;

                }
            }
        }
        System.out.println(total);

    }
}
leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • You can save value in a file, and then retrieve it every time, when your function is called – Anton Tokmakov Jun 08 '21 at 12:02
  • 1
    I believe you whant i to "keep track" of all id created, even after the program stopped. You would need to save this information [into a file for instance](https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it) and then [read it](https://stackoverflow.com/questions/2049380/reading-a-text-file-in-java) at startup. Also, I can see that you are trying to prepend zeros to integer depending on its length. You should take a look at [this](https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left) – Raphallal Jun 08 '21 at 12:08

3 Answers3

2

You can declare the variable int i = 10; outside of the main Method, say for example, where the method will be called. So that the value will not be changed, everytime you call the main method.

EDIT: If you want to read the previous value of i after the new start of an application then it should be stored in a database or else a file, that persists the value of i even after the Program is not running.

lepaf78691
  • 25
  • 8
  • but i want for next time the value of i to be 11 but using your method i am always getting 10 whenever i am running the program – naman kapoor Jun 08 '21 at 12:05
1

Only way to persist value of i every time you run your application is by saving it to drive or to DB or something else you can come up with. Every time application exits all data is lost, you need to store it outside of your application.

Probably the easiest way is to use java.nio and save the value to file every time you increase the value of i then read the file every time you start application.

For something different you'll probably need much more time, but I have no idea what are your requirements.

Filip
  • 609
  • 7
  • 18
0

I suggest you use the variable statistics as following :

public class TestId {
    static int i = 10;
    public static String total(){
        String start = "CUST";
        String total = "";
        if (10 <= i && i < 100) {
            total = start + "000" + i;
            i++;
        } else {

            if (i <= 100 && i < 1000) {
                total = start + "00" + i;
                i++;
            } else {
                if (i <= 1000 && i < 10000) {
                    total = start + "0" + i;
                    i++;
                } else {

                    total = start + i;
                    i++;

                }
            }
        }

        return total;
    }

    public static void main(String[] args) {
        System.out.println("first call "+total()); // output: CUST00010
        System.out.println("second call "+total()); // output: CUST00011
        System.out.println("second call "+total()); // output: CUST00012
    }
}
snd
  • 133
  • 6