ID generator

package Utiliarios; import java.util.Random; class Id { private int longitud; private int decoder; private String[] idArray; public Id(int longitud){ this.longitud = longitud; } public void generarId(int longitud){ boolean flagIntOrString = true; idArray = new String[longitud]; for(int i=0;i<longitud;i++) { Random num = new Random(); if (num.nextDouble() > 0.5) { int c = (int) ((num.nextDouble() * 122) - 95); char cc = (char) c; idArray[i] = String.valueOf(cc); }else{ int c = (int) ((num.nextDouble() * 122) - 95); idArray[i] = String.valueOf(c); } } } public void printId(){ String cadenaId = null; for (int i=0;i<this.longitud;i++){ cadenaId = new StringBuilder().append(this.idArray[i]).toString(); } System.out.println(cadenaId); } }
It's very simple, is a id generator. A number combined with characters array. Mostly used for keys and unique IDs.

2 Responses

It's a very cool homemade id gen :). In java you can use UUID either https://docs.oracle.com/javase/7/docs/api/java/util/UUID.html if the project permits.
Just a little ideia in code pattern matter, I'd just change the constructor to private e make generateId a static factory (Bloch, Effective Java).
i.e.
public static Id generateId(int logitud){
Id id = new Id(longitud);
id.idArray = new String[logintud];
....
return id;
}
this helps to build more semantic sentences :D i.e. Id.generateId(10);
Just an ideia (I like to share and discuss code pattern stuff hehehe):D
Best regards!
Hi Kim :). Thanks, now it's updated and tested. And, yes, this code your help to make semantic sentences. I'm using for create UNIQUE id for database with 500 registered persons. Regards!

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.