java - 从解析 String 到 double 的转换返回 0.0 :

标签 java arrays parsing type-conversion double

这是我就该主题提出的最后一个问题的后续。但这是一个不同的问题。

我的代码正在工作,除了它使用 copyOfRange 复制某种地址。由于某种地址,它总是返回 0.0,而不是数组 getBits 的部分。

有人可以扫描一下这个并提出建议吗?我对此感到疯狂(这不是作业)。

package runTests;

import java.util.Arrays;

public class runTestGetBinaryStrands {
    protected static int getBits[] = {1,0,1,1,0,1,0,0,0,1,1,0,1,0,1,0};
    double numerator, denominator, x, y;

    public static void main (String[] args){
        runTestGetBinaryStrands test = new runTestGetBinaryStrands();
        test.getNumber(null, getBits);
    }
    /*NOTE OF THIS FORLOOP:     * Divided the bits array in half & convert two different binary values to a string  * I parsed the string to an int value, which can be put saved to a double and be treated like a decimal value.  * I got the first 8 elements and stashed them into numerator, and did the same for denominator for the remaining array bits.    * * The chromosome has one binary string, made up of a bunch of smaller parts.* You use getNumber in the chromosome to get out the values of the parts.     **/     

    public void getNumber(String convert, int[] tempBinary){       
        for (int i = 0; i < getBits.length; i++){
            for(int j = 0; j < getBits.length; j++){ //start at index 0 to 7 = 8.
                tempBinary = Arrays.copyOfRange(getBits, 0, 7); //Get first set of 8 elements.
                convert = tempBinary.toString();
                System.out.println(convert);
                try{
                    numerator = Integer.parseInt(convert); //converts string to one whole section in
                }catch (NumberFormatException ex){
                }
                System.out.println("See Numerator's value: " + numerator);                          
                tempBinary= Arrays.copyOfRange(getBits, 8, 15); //Get Second set of 8 elements.
                convert = tempBinary.toString();
                try{
                    denominator = Integer.parseInt(convert); //converts string to one whole section in
                }
                catch (NumberFormatException ex){
                }                           
                System.out.println("See Denominator's value: " + denominator);
            } 
        } 
    } 
}

最佳答案

将行 convert = tempBinary.toString(); 替换为:

    convert = "";
    for(int bin : tempBinary){
      convert += bin;
    }

这应该可以让您的转换正常进行。

关于java - 从解析 String 到 double 的转换返回 0.0 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337908/

相关文章:

parsing - 用于上下文无关文法的FIRST和FOLLOW集的计算算法

java - 如何在java中跨面板拖动组件

java - Java自定义类加载器中调用findClass时类名的规范

java - 你如何传递数组?

c - 动态字符串数组不起作用

c# - 没有字符串拆分的解析

java - 从Socket读取数据的问题

java - Android 应用程序未在 Virtual Box 上运行吗?

javascript - 在哪个版本的 JavaScript 中为数组引入了括号表示法?

ios - 如何通过php web服务成功登录