简体   繁体   中英

Conversion from parsing a String to double is returning 0.0:

This is a follow up on the last question I made regarding this topic. It's a different issue though.

My code is working, except it's copying some sort of address using the copyOfRange. It always returns 0.0 due to an address of some sort, instead of the section of the array getBits.

Can someone please scan this is and make a suggestion? I am going crazy over this (it's not an assignment).

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);
            } 
        } 
    } 
}

Replace the lines convert = tempBinary.toString(); with:

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

That should get your conversion working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM