Code error I can’t find - Create CNPJ (Java)

Asked

Viewed 114 times

0

Good morning guys, could someone help me?

I have this Java code that must create valid CNPJ number. However, the answer is Class GeradorCnpj when should I see the number.

Follows the code:

public class GeradorCnpj {
    public String geraCNPJ() throws Exception {
     int digito1 = 0, digito2 = 0, resto = 0;
     String  nDigResult;
     String numerosContatenados;
     String numeroGerado;
     Random numeroAleatorio = new Random();
      //numeros gerados
     int n1 = numeroAleatorio.nextInt(10);
     int n2 = numeroAleatorio.nextInt(10);
     int n3 = numeroAleatorio.nextInt(10);
     int n4 = numeroAleatorio.nextInt(10);
     int n5 = numeroAleatorio.nextInt(10);
     int n6 = numeroAleatorio.nextInt(10);
     int n7 = numeroAleatorio.nextInt(10);
     int n8 = numeroAleatorio.nextInt(10);
     int n9 = numeroAleatorio.nextInt(10);
     int n10 = numeroAleatorio.nextInt(10); 
     int n11 = numeroAleatorio.nextInt(10); 
     int n12 = numeroAleatorio.nextInt(10); 
     int soma = n12*2 + n11*3 + n10*4 + n9*5 + n8*6 + n7*7 + n6*8 + n5*9 + n4*2 + n3*3 + n2*4 + n1*5;
     int valor = (soma / 11)*11;
     digito1 = soma-valor;
      //Primeiro resto da divisão por 11.
     resto = (digito1 % 11);
     if(digito1 < 2){
      digito1 = 0;
     }
     else {
       digito1 = 11-resto;
     }
     int soma2 =  digito1*2 + n12*3 + n11*4 + n10*5 + n9*6 + n8*7 + n7*8 + n6*9 + n5*2 + n4*3 + n3*4 + n2*5 + n1*6 ;
     int valor2 = (soma2 / 11)*11;
     digito2 = soma2-valor2;
      //Primeiro resto da divisão por 11.
     resto = (digito2 % 11);
     if(digito2 < 2){
      digito2 = 0;
     }
     else {
       digito2 = 11-resto;
     }
     //Conctenando os numeros
     numerosContatenados = String.valueOf(n1) + String.valueOf(n2) +"."+ String.valueOf(n3) + String.valueOf(n4) +
                           String.valueOf(n5) +"."+ String.valueOf(n6) + String.valueOf(n7) +String.valueOf(n8)+"/"+
                           String.valueOf(n9) + String.valueOf(n10) + String.valueOf(n11) +
                           String.valueOf(n12)+"-";
     //Concatenando o primeiro resto com o segundo.
      nDigResult = String.valueOf(digito1) + String.valueOf(digito2);
      numeroGerado = numerosContatenados+nDigResult;
      System.out.println("Digito 2 ->"+digito2);
      System.out.println("CNPJ Gerado " + numeroGerado);
      return numeroGerado;
    }
    public String mostraResultado() throws Exception{
     String resultadoCNPJ = geraCNPJ();
     return resultadoCNPJ;
    }
}
  • You have prompted an object from this class to use the methods of this class?

  • I don’t see any error in the code, you can see the result I got using it here: https://ideone.com/gwfdIQ

  • Romulus, I used his example and continues to return "Class Geradorcnpj". Strange...

1 answer

0


I tested the code and invoked the following main method:

public static void main(String[] args) throws Exception {
    GeradorCnpj gerador = new GeradorCnpj();
    System.out.println(gerador.mostraResultado());
}

The output was as follows:

Type 2 ->0 CNPJ Generated 09.231.247/0962-80 09.231.247/0962-80

  • Patricia, I used your example.. Continue returning "Class Generator CNPJ"... What will be that can be?

  • How is compiling the code and executing it?

  • Patricia, I used the following code:

  • The character limit exceeds. I cannot include the code. But the code is the same as in my post. ?

  • Using an IDE to compile and run the application?

  • Patrícia, I’m using the Jmeter tool (automation).

  • 1

    So I think this has to do with setting up the test in Jmeter. I was able to replicate without problems. You can follow the following example, maybe it will help: https://www.linkedin.com/pulse/using-custom-jar-beanshell-sampler-jmeter-lakshmi-narayan/

Show 2 more comments

Browser other questions tagged

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