Class creation that returns a String

4

Hello everyone, I'm a beginner in Java and I'm having a problem, I'm trying to create a class that returns a String . I create the file .java and paste my code but it gives the following error in the declaration String : Syntax error on token "String", @ expected

This is the complete code of the class (there is also an error in a ; soon in the beginning and the last } in the end):

    package com.example.minhaslicencasnobre;

    import java.io.UnsupportedEncodingException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import android.app.AlertDialog;
    import android.content.DialogInterface;

    public String GenerateRegCod(String[] Code, String Hash) throws NoSuchAlgorithmException, UnsupportedEncodingException{ //Em String fica sublinhado indicando erro

    int i;

    //Cria a criptografia MD5
    MessageDigest md5 = MessageDigest.getInstance("MD5");   

    String newCode = "";    //No ';' fica sublinhado indicando erro 

    //For para tirar os "-"
    for(i = 0; i < Code.length; i++)
    {
        char c = Code[i].charAt(0); 
        if(c != '-'){
            newCode += Code[i].toString();
        }
    }

    newCode = newCode.toUpperCase();

    //Verifica se a variável newCode contém somente os 16 caracteres hexa da variável Code
    if(newCode.length() != 16)
    {
        //Mostra mensagem: código inválido
        AlertDialog alerta;
        //Cria o gerador do AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //define o titulo
        builder.setTitle("Erro");
        //define a mensagem
        builder.setMessage("Código inválido.");
        //define um botão como positivo
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {          
               //conteudo do botão
            }
        });
        //cria o AlertDialog
        alerta = builder.create();
        //Exibe
        alerta.show();

        return "";
    }

    byte[] appDeviceID = newCode.getBytes("ASCII");

    byte[] appCodeToHash = Hash.getBytes("ASCII");


    if(appCodeToHash.length < appDeviceID.length)
        return "";

    byte[] toHash = new byte[appDeviceID.length * 2];

    for(i = 0; i < appDeviceID.length; i++)
    {
        toHash[i * 2] = appDeviceID[i];
        toHash[i * 2 + 1] = appCodeToHash[i];
    }       


    byte[] dataRegCodeToCompare = md5.digest(toHash);

    int[] vetor = new int [dataRegCodeToCompare.length];

    for(i=0;i < dataRegCodeToCompare.length;i++){
    vetor[i] = dataRegCodeToCompare[i];
    }

    for(i=0; i < vetor.length; i++)
    {
        if(vetor[i] < 0)
            vetor[i] = vetor[i]+256;
        else
            vetor[i]=vetor[i];  
    }   

    StringBuffer hexString = new StringBuffer();

    for (i=0; i<vetor.length; i++){

        if(vetor[i]>=0 && vetor[i]<=9){
            hexString.append("0").append(Integer.toHexString(0xFF & vetor[i])).append("-");
        }
        else
    hexString.append(Integer.toHexString(0xFF & vetor[i])).append("-");
    }

    char[] s = (hexString.substring(0, hexString.length()-1).toUpperCase()).toCharArray();


    StringBuilder sb = new StringBuilder();


    for(i = 0; i < s.length; i+=3)
    {     
        sb.append(s[i+1]); // vamos usar somente a segunda parte
    }

    return sb.toString().toUpperCase();     
}//No '}' fica sublinhado indicando erro
    
asked by anonymous 18.05.2015 / 21:09

1 answer

3

The solution is very simple, your method GenerateRegCod needs to belong to a class.
Forexample:

//Imports...publicclassMinhaClasseIncrivel{publicStringGenerateRegCod(String[]Code,StringHash)throwsNoSuchAlgorithmException,UnsupportedEncodingException{//Códigodométodo}}

Andthen,insidethemainmethod,justgetaninstanceofMinhaClasseIncriveltobeabletocallthemethod:

publicstaticvoidmain(String...args){MinhaClasseIncrivelinstanciaDaMinhaClasse=newMinhaClasseIncrivel();Stringm=instanciaDaMinhaClasse.ClasGenerateRegCod(2,"PRINCIPIODACORRESPONDENCIA");
}
    
18.05.2015 / 21:38