简体   繁体   中英

How to create an unique ID in java

So i have to do some work and i am still pretty new to Java we just started learning it. And we only use expressions like "pattern", "match", arraylist stuff pretty basic to most of you i am sure. and Its a work for a class called data integration where we basically put the wikipedia link and we put regular expresions and if you search specific authors it appears their information and we save all the data in an XML file. and in that xml we also have to create an ID to every single author that we search. for example if you search first JK_ROWLING, she is going to be 01 and then we search VERONICA_ROTH so she is 02 and so on, and of course save it also in the files.

here is some code for you guys to understand the workd and the regular expressions so like in this piece i search any birth date of any author

public static String procuraDataNascimento(String nome_escritor) throws IOException{

        String link = "https://pt.wikipedia.org/wiki/" + nome_escritor;
        String pesquisa =""; 
        HttpRequestFunctions.httpRequest(link,pesquisa,"ESCRITORES.txt");
        String Nasc_er0 = "<td scope=\"row\" style=\"vertical-align: top; text-align: left; font-weight:bold; padding:4px 4px 4px 0\">Data de nascimento</td>";
        String Nasc_er = "<td style=\"vertical-align: top; text-align: left; padding:4px\"><a href=\"/wiki/(.+)#Nascimentos\" title=\"(.+)\">(.+)</a> de <a href=\"/wiki/(.+)\" title=\"(.+)\">(.+)</a></td>";
        String Nasc_er2 = "<td style=\"vertical-align: top; text-align: left; padding:4px\">(.*)<a href=\"/wiki/(.+)#Nascimentos\" title=\"(.+)\">(.+)</a> de <a href=\"/wiki/(.+)\" title=\"(.+)\">(.+)</a>(.*)</td>";
        String Nasc_er3 = "<td style=\"vertical-align: top; text-align: left; padding:4px\">(.+)</td>";
        String Nascimento = null;
        Scanner ler = new Scanner(new FileInputStream("ESCRITORES.txt"));
        Pattern p0 = Pattern.compile(Nasc_er0);
        Pattern p = Pattern.compile(Nasc_er);
        Pattern p2 = Pattern.compile(Nasc_er2);
        Pattern p3 = Pattern.compile(Nasc_er3);
        while (ler.hasNextLine()) {
            String linha = ler.nextLine();
            Matcher m = p0.matcher(linha);
            if(m.find()){
                linha = ler.nextLine();
                Matcher t = p.matcher(linha);
                Matcher r = p2.matcher(linha);
                Matcher q = p3.matcher(linha);
                if (t.find()) {
                    Nascimento = t.group(2) + " de " +  t.group(5);
                } 
                else if (r.find()) {
                    Nascimento = r.group(3) + " de " +  r.group(5);   
                }
                else if(q.find()){
                    Nascimento = q.group(1);
                }
            }
        }
        ler.close();
        return Nascimento;
    }

and when i put this

{

String x = Wrapper_escritores.procuraDataNascimento("Roberto_Bolaño");

System.out.println(x);

}

it shows the birth date of Roberto Bolãno for example. I also have already a class for the authors with constructors and gets and sets. Can you guys help? Do you know it how to do seeing that the only things that we have learned so far are pretty basic? Thank you!!

You could generate a UUID a class that represents an immutable universally unique identifier (UUID) , something like

String name = "Roberto Bolãno";
UUID uuid = UUID.nameUUIDFromBytes(name.getBytes(Charset.forName("UTF-8")));
System.out.println(uuid.toString());

Which outputs

4fb97aea-d741-3d78-a037-0eaa8848fc7a

Alternatively, create a Map<Integer, String> (see Tutorial ) and increment a counter when you add a new author (if your application needs to be restart-able, you will also need to persist these values).

Use the link as the ID. Assuming that Wikipedia URLs don't change for authors, they are stable over time. Plus, different authors are guaranteed to have different Wikipedia URLs.

If you need the ID to be alphanumeric, run the link through a cryptographic hash function like SHA-1 and print the resulting byte[] as a hex string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM