[Solved]
I’ve searched many forums as well as Stack Overflow’s questions for converting an ever changing random number to a string but I couldn’t find a solution to my specific problem.
How do I go about converting customerID to a string?
public class UniqueCustomerNumber {
private int customerID;
public void setCustNum(int cNumber) {
Random x = new Random();
cNumber = x.nextInt(800000001) + 100000000;
customerID = cNumber;
}
public int getCustNum() {
return customerID;
}
}
Use
Integer.toString(int i)It’s a static method inIntegerclass that takes oneintas an agument and returns its textual representation asString.Code:
public class UniqueCustomerNumberTest {
Output:
A side note: Using this method may not be in compliance with your class name,
UniqueCustomerNumber. I know that the probability is really small, but it could theoretically happen that 2 customers have the same IDs if you generate them this way. A much simpler approach is to start assigning cutomers their IDs starting from some number, 1, for example.On each assignment, you’d increase your counter by 1. The downside is that you’d need to keep that counter saved somewhere (DB, txt file, w/e).