Class creation that returns a String

Asked

Viewed 118 times

4

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

This is the complete code of the class (there is also an error in a ; right at 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
  • It wouldn’t be enough to create a class like this: public class MinhaClasse { public String GenerateRegCod.... }?

  • Unable to create a class that returns a string.

1 answer

3


The solution is quite simple, your method GenerateRegCod must belong to a class. .
For example:

// Imports...

public class MinhaClasseIncrivel {

   public String GenerateRegCod(String[] Code, String Hash) throws
                 NoSuchAlgorithmException, UnsupportedEncodingException {
        // Código do método
   }
}

And then, within the main method, just get an instance of MinhaClasseIncrivel to call the method:

public static void main(String... args){
   MinhaClasseIncrivel instanciaDaMinhaClasse = new MinhaClasseIncrivel();
   String m = instanciaDaMinhaClasse.ClasGenerateRegCod(2,"PRINCIPIODACORRESPONDENCIA");
}
  • 1

    It worked! Thank you! Oh, one more thing, in this line of code in the show part AlertDialog made a mistake: AlertDialog.Builder builder = new AlertDialog.Builder(this); The mistake is: The constructor AlertDialog.Builder(ClasGenerateRegCod) is undefined

  • And when it comes to calling, there’s a mistake too: The method GenerateRegCod(String[], String) is undefined for the type ClasGeradorLicencas

  • How are you calling this method @kaamis?

  • String m=ClasGenerateRegCod(cod2,"PRINCIPIODACORRESPONDENCIA"); So @Renan

  • @kaamis vc need to obtain an instance of this class in order to use the method.

  • could you give me a practical example? I hardly understand your comments

  • But I had already edited the answer with the example, did you see after editing? @kaamis

  • no... now I saw, the page had not updated. Brigadão!!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.