Method return in Java

3

I do not know anything about Java. I would like to know how to get the return value of the dv variable that is inside the Base10() method and use it in 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;
        } 
     }
 }
    
asked by anonymous 12.01.2018 / 17:51

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 output is 6.

See here working on ideone.

As the question has been edited, here's some more information:

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

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

If it'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 Base10 ):

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

However, an instance of Barra represents nothing. The Barra method does not require any instances to operate. So unless you reorganize things in some other way, it would be best to leave the% static method even.

One other though is that the method should be called Base10 . The Java naming convention dictates that method names should / should be initialized with a lowercase letter. See more about this here .

    
12.01.2018 / 17:55
0
public static void main(String[] args) {

    String codBarras = "23793.44308.90010.000041.33001.250001.3.52830000008091";        
         int dv = Base10(codBarras){ 
}
    
12.01.2018 / 17:56
0

Call the method and assign it to a variable:

x = Base10(String num)

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

    
12.01.2018 / 17:55