Problem in Java! (impression of a value returned within an if)

3

It turns out that I have some problem at the time of continuing with my code which is a task of u, I usually use Javascript, and I'm new to Java, so I do not know many basic things, but having experience programming in another language such as Javascript, I thought that using more or less the same logic that I use in Javascript to do my projects could be used in Java, as it seems that it does not seem ...

The problem is summarized as follows:

I have a separate function called binaryAnt, which returns a value. I then have below that function (Outside the binaryAnt function) my main function, (The one that contains String [] args) called main, it turns out that everything goes great when I use it with its respective parameters in the main, and then a once executed the binaryAnt function with their respective parameters I print the value that is generated, when I do it everything goes perfect gives me the correct value and everything, but ...

If I do something as simple as putting the line of code where I am going to print such value returned in an if, I do not print anything !, and that I verified that it was really entering the if, and I still can not explain why it does not work ...

Any ideas?

here the code:

import java.util.Scanner;
import java.io.PrintStream;

public class Conversor_Binario {

    public static int binarioAInt(String binario,int[] limits,String[] arrBinario) {

        int numAS = 0;
        int j = 0;
        int n = 1;

        for(int i = arrBinario.length-1 ; i >= 0 ; i--) {

            if(i == arrBinario.length-1) {

                n = 1;

            }else {

                n = n*2;

            }

            limits[i] = n;

        }

        while(j < binario.length()) {

            arrBinario[j] = binario.substring(j,j+1);

            j++;

        }

        for(int k = 0; k < arrBinario.length ; k++) {

            if(arrBinario[k].indexOf("1") != -1) {

                numAS = numAS + limits[k];

            }

        }

        return numAS;

    }

    public static void main(String[] args) {

        PrintStream imprimir = System.out;
        Scanner escanear = new Scanner(System.in);
        String typeConversion;
        String mayuscTypeConversion;

        String entradaUsuario;

        imprimir.print("¡Bienvenido al conversor binario!, escribe un numero en binario o decimal y lo convertiremos a");
        imprimir.print(" su correspondiente equivalente.");
        imprimir.println("");
        imprimir.println("");
        imprimir.print("¿Qué deseas convertir?, escribe D (para convertir de decimal a binario), escribe B (para convertir");
        imprimir.print(" de binario a decimal).");
        imprimir.println("");
        imprimir.println("");

        typeConversion = escanear.nextLine();
        mayuscTypeConversion = typeConversion.toUpperCase();

        imprimir.println("");
        imprimir.println("Ahora dame el valor a convertir:");
        imprimir.println("");

        entradaUsuario = escanear.nextLine();
        String[] arregloBinario = new String[entradaUsuario.length()];

        imprimir.println("");

        int[] limites = new int[entradaUsuario.length()];
        //Aquí en este condicional esta el problema... si dejas el contenido fuera del condicional si funciona correctamente.
        if(mayuscTypeConversion == "B") {

            imprimir.println(binarioAInt(entradaUsuario,limites,arregloBinario));

        }

        escanear.close();

    }


}
    
asked by Riven 04.09.2018 в 22:40
source

1 answer

3

The problem is simple, you should not use the == operator to compare strings, you should use the equals () .

To convert from Integer to Binary, you can do it using the method:

Integer.toBinaryString ()

This would be the code:

if(mayuscTypeConversion.equals("B")) {

    imprimir.println(binarioAInt(entradaUsuario,limites,arregloBinario));

}else if(mayuscTypeConversion.equals("D")){

    imprimir.println(Integer.toBinaryString(Integer.parseInt(entradaUsuario)));

}else{
    imprimir.println("No selecciono formato de conversión.");
}

What happened was that he never really went to make the impression of value.

    
answered by 04.09.2018 / 22:48
source