It turns out that I have a certain problem when it comes to continuing with my code, which is a task of the u, I normally use Javascript, and I'm new to Java, so I don't know many basic things, but having programming experience in another language such as Javascript, I thought that using more or less the same logic that I use in Javascript to make my projects could be used in Java, as it turns out that apparently not...
The problem in summary is the following:
I have a separate function called binaryAInt, which returns a value. then I have below that function (Outside the binaryAInt 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 main, and then a Once the binaryAInt function is executed with its respective parameters, I print the value that is generated, when I do it like this, everything goes perfectly, it 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 return value inside an if, it does not print anything! works...
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();
}
}
The problem is simple, you shouldn't use the operator
==
to compare strings, you should use the equals() method .To convert from Integer to Binary you can do it using the method:
Integer.toBinaryString()
This would be the code:
What happened is that he never really entered to make the impression of the value.