【问题标题】:Having trouble converting a string into an integer将字符串转换为整数时遇到问题
【发布时间】:2022-02-02 17:53:22
【问题描述】:
public static int[] booleanToBinary(boolean[] b) {
    int[] arr = new int[b.length];
    for(int i = 0; i < b.length; i++) {
        if(b[i] == true) {
             arr[i] = 1;
        }
        else{arr[i] = 0;};
        }
    
    return arr;
}



public static int binaryToInt(boolean[] b) {
    int[] a = booleanToBinary(b);
    String c = Arrays.toString(a);
    System.out.println(c);
    int decimal = Integer.parseInt(c, 2);
    
        System.out.println(decimal);
    

    
    return decimal;
    
}

 public static void main(String[] args) {
    boolean[] test = {false, true, false, true};
    System.out.println(Arrays.toString(booleanToBinary(test)));
    System.out.println(binaryToInt(test));
    
}

块引用 我正在尝试将二进制值转换为整数值,并且我正在尝试使用 binaryToInt 方法来执行此操作,并且正在发生 NumberExceptionFormat,我知道当 java 无法将字符串转换为整数时会发生此错误,可以有人帮我解决这个错误

【问题讨论】:

  • int decimal = Integer.parseInt(c, 2); c 将类似于"[1,0,1...]" 也许您的意思是先将列表加入字符串(“101 ...”)
  • 在这里找到一些解决方案:Java: join array of primitives with separator 除非您不想要分隔符。只是一个空字符串 ("")。例如:String c = IntStream.of(a).mapToObj(i -&gt; String.valueOf(i)).collect(Collectors.joining());
  • @JohnnyMopp ,是的,这是个好主意,我可以将字符串 c 修改为 String c = a.toString();,但它显示了 [I@372f7a8d
  • 您必须将数组加入字符串 - 请参阅我之前评论中的链接。 或者,让booleanToBinary() 返回一个字符串而不是一个数组。例如使用StringBuilder 或只是简单的串联。

标签: java numberformatexception


【解决方案1】:

要将布尔值列表转换为等效的 int,请尝试这样。无需使用任何字符串或整数解析方法。

// 100111 the value = 39
boolean[] b = {true, false, false, true, true, true};
int v = binaryToInt(b);
System.out.println(v);

打印

39
  • 先将小数乘以 2
  • 然后根据需要添加 1 或 0。
  • 继续、乘法和加法
public static int binaryToInt(boolean[] bools) {
    int decimal = 0;
    for (boolean b : bools) {
        decimal = decimal * 2 + (b ? 1 : 0);
    }
    return decimal;
}

同样,将布尔数组转换为 1 或 0 数组。

public static int[] booleanToBinary(boolean[] bools) {
    int[] arr = new int[bools.length];
    int i = 0;
    for (boolean b : bools) {
        arr[i++] = b ? 1 : 0;
    }
    return arr;
}

请注意,如果您只想将布尔数组转换为二进制字符串,这将起作用。

public static String booleanToBinaryString(boolean[] bools) {
    int[] arr = new int[bools.length];
    String result = "";
    for (boolean b : bools) {
        result += b ? 1 : 0;
    }
    return result;
}

System.out.println(booleanToBinaryString(b));

打印

100111

【讨论】:

    【解决方案2】:

    使用Arrays.toString 将0 和1 的数组转换为字符串后,可以使用String::replaceAll 从字符串中删除所有非零和非一:

    public static int binaryToInt(boolean[] b) {
        int[] a = booleanToBinary(b);
        String c = Arrays.toString(a).replaceAll("[^01]", );
        System.out.println(c);
        int decimal = Integer.parseInt(c, 2);
        
        System.out.println(decimal);
        return decimal;    
    }
    

    然后测试:

     boolean[] test = {false, true, false, true};
    System.out.println(Arrays.toString(booleanToBinary(test)));
    System.out.println(binaryToInt(test));
    

    打印:

    [0, 1, 0, 1]
    0101
    5
    5
    

    但是,这里的位顺序是颠倒的,因为数组是从 0 到 n(从低位开始)打印的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多