Return method in Java

Asked

Viewed 430 times

3

I don’t know anything about Java. I would like to know how to get the return value of the variable dv that is within the method Base10() and use it on main. Could someone help me?

public class Boleto {

public static void main(String[] args) {

    String codBarras = "23793.44308.90010.000041.33001.250001.3.52830000008091";        

}
    public class Barra{
        public int Base10(String num){  
            //variáveis de instancia
            int soma = 0;
            int resto = 0;
            int dv = 0;
            String[] numeros = new String[num.length()+1];
            int multiplicador = 2;
            String aux;
            String aux2;
            String aux3;

            for (int i = num.length(); i > 0; i--) {            
                //Multiplica da direita pra esquerda, alternando os algarismos 2 e 1
                if(multiplicador%2 == 0){
                    // pega cada numero isoladamente  
                    numeros[i] = String.valueOf(Integer.valueOf(num.substring(i-1,i))*2);
                    multiplicador = 1;
                }else{
                    numeros[i] = String.valueOf(Integer.valueOf(num.substring(i-1,i))*1);
                    multiplicador = 2;
                }
            }  
            // Realiza a soma dos campos de acordo com a regra
            for(int i = (numeros.length-1); i > 0; i--){
                aux = String.valueOf(Integer.valueOf(numeros[i]));
                if(aux.length()>1){
                    aux2 = aux.substring(0,aux.length()-1);             
                    aux3 = aux.substring(aux.length()-1,aux.length());
                    numeros[i] = String.valueOf(Integer.valueOf(aux2) + Integer.valueOf(aux3));             
                }
                else{
                    numeros[i] = aux;           
                }
            }
            //Realiza a soma de todos os elementos do array e calcula o digito verificador
            //na base 10 de acordo com a regra.     
            for(int i = numeros.length; i > 0 ; i--){
                if(numeros[i-1] != null){
                    soma += Integer.valueOf(numeros[i-1]);
                }
            }
            resto = soma%10;
            dv = 10 - resto;
            //retorna o digito verificador

            return dv;
        } 
     }
 }
  • I saw that you edited the question. Because of this, I edited the my answer also.

3 answers

5


Just do this:

public static void main(String[] args) {
    String codBarras = "23793.44308.90010.000041.33001.250001.3.52830000008091";        
    String codBarrasLimpo = codBarras.replace(".", "");
    int dv = Base10(codBarrasLimpo);
    System.out.println(dv);
}

The exit is 6.

See here working on ideone.


As the question was edited, here is some more information:

The String codBarrasLimpo = codBarras.replace(".", ""); serves to remove points from the String. The code of the method Base10 does not know how to deal with them, and so it is important to remove them before calculating base 10. A String original (codBarras) is preserved, which means that this operation is not destructive, because another String (to codBarrasLimpo) without the dots is created without the old (codBarras) is damaged.

The above code is possible if the method Base10 is static and in the same class.

If he’s in another class, still static, then you use this:

int dv = Barra.Base10(codBarrasLimpo);

If it is not static, you will need an instance of the class that surrounds it (in this case Barra):

Barra b = new Barra();
int dv = b.Base10(codBarrasLimpo);

However, an instance of Barra does not represent anything. The method Base10 does not need any instance to operate. So unless you reorganize things in some other way, it would be best to leave the method Base10 even static.

Another is that the method should be called base10. The Java naming convention dictates that method names should/should be started in lower case. See more about this here.

  • 1

    Thanks man, it helped a lot!

0

public static void main(String[] args) {

    String codBarras = "23793.44308.90010.000041.33001.250001.3.52830000008091";        
         int dv = Base10(codBarras){ 
}

0

Call the method and assign it to a variable:

x = Base10(String num)

Soon 'x' will receive the return of the method;

  • Base10 is static, there is no need to create an object

  • I didn’t see that part

  • @Victor I edited the code, actually the Base10 method is not static.

Browser other questions tagged

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