Подскажите, где может крыться ошибка? Я уже и return'ы менял, но не особенно помогло.
Задача отрабатывает успешно по заданным цифрам, но ошибку я найти не могу((
package com.javarush.task.pro.task09.task0906;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.regex.Pattern;
/*
Двоичный конвертер
*/
public class Solution {
public static void main(String[] args) {
int decimalNumber = Integer.MAX_VALUE;
System.out.println("Десятичное число " + decimalNumber +
" равно двоичному числу " + toBinary(decimalNumber));
String binaryNumber = "1111111111111111111111111111111";
System.out.println("Двоичное число " + binaryNumber +
" равно десятичному числу " + toDecimal(binaryNumber));
}
public static String toBinary(int decimalNumber) {
if (decimalNumber <=0) {
return "";
}
String binaryNumber = ""; // a string to store the value and add a new value after each cycle
while (decimalNumber != 0) {
binaryNumber = decimalNumber % 2 + binaryNumber; //here we are getting the number (0 or 1)
//here we are storing binary numbers
decimalNumber = decimalNumber / 2; //decreasing our decimal
}
return binaryNumber;
}
public static int toDecimal(String binaryNumber) {
if (binaryNumber == null) {
return 0;
}
int decimal = 0; //here we create a decimal variable, so we can update it after each cycle.
// it has to be 0;
char[] array = binaryNumber.toCharArray(); //converting string to char array
int[] numbers = new int[array.length]; //creating an int array so we can have a number to work with
for (int i = 0; i < array.length; i++) { //creating an int array here
int temp = array[i] - '0';
numbers[i] = temp;
}
for (int j = 0; j < numbers.length; j++) { //converting Binary number into decimal
int pow = (int)Math.pow(2,j);
decimal = decimal + numbers[j] * (int)pow;
}
return decimal;
}
}