将数组转换为双精度

时间:2016-04-29 14:15:57

标签: java double

抱歉,我是编码的新手,我正在尝试将数组转换为双精度数,但我想跳过数组的第一行。

我有以下代码

    public void organisedata()
{


    try (BufferedReader readbuffer = new BufferedReader(new FileReader(filepath))) {
        String strRead;
        int count=0;

        //strRead = readbuffer.readLine() != null;

        while ((strRead = readbuffer.readLine()) != null) {
            count++;

            String splitarray[] = strRead.split("\t");
            String firstentry = splitarray[0];
            String secondentry = splitarray[1];
            String thirdentry = splitarray[2];
            String fourthentry = splitarray[3];
            String fifthentry = splitarray[4];
            String sixthentry = splitarray[5];
            String seventhentry = splitarray[6];
            String eighthentry = splitarray[7];
            String ninthentry = splitarray[8];
            String tenthentry = splitarray[9];
            String eleventhentry = splitarray[10];
            String twelvethentry = splitarray[11];
            String thirteenthentry = splitarray[12];
            String fourteenthentry = splitarray[13];
            String fifthteebthentry = splitarray[14];
            String sixteenthentry = splitarray[15];
            String seventeenthentry = splitarray[16];
            String eightteenhentry = splitarray[17];

            System.out.println(firstentry + " " + secondentry + " " +thirdentry + " " + fourthentry+ " " + fifthentry + " " + sixthentry + " " +seventhentry + " " + eighthentry+ " " +ninthentry+ " " +tenthentry+ " " +eleventhentry+ " " +twelvethentry+ " " +thirteenthentry+ " " +fourteenthentry+ " " +sixteenthentry+ " " +seventeenthentry+ " " +eightteenhentry);


            int length =splitarray.length;
            double[] Exprs_Val=new double[length] ;
            for (int i = 1; i< length+1; i++)
            { System.out.println (splitarray[i]);
                Exprs_Val[i-1]=Double.parseDouble(splitarray[i]);

            }
            System.out.println( "Double Array is" +Exprs_Val+"");

        }

    }   catch (IOException e) {
        System.err.format("IOException: %s%n", e);
    }
}

这段代码编译,第一部分是spilting我的数组和他们我想要隐藏到双打,但是当我运行它时,我在终端中得到以下错误。

java.lang.NumberFormatException: For input string: "GSM119944"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at serialanova.organisedata(serialanova.java:142)
at serialanova.<init>(serialanova.java:41)

输入文件

ProbeSetID GSM119944 GSM119945 GSM119946 GSM119947 GSM119948 GSM119949 GSM119950 GSM119951 GSM119952 GSM119953 GSM119954 GSM119955 GSM119956 GSM119957 GSM119958 GSM119959 GSM119960 1007_s_at 11.37651913 11.82674322 11.12302175 11.7434386 12.17296072 11.52200918 11.28840678 11.36454405 11.783231 12.10269679 12.14193446 12.14167191 11.54116091 12.06920648 11.52945627 9.692066334 11.24298832 1053_at 7.270398138 7.534450178 7.169297224 7.730833029 6.728913697 7.033900213 8.152550207 7.357942158 7.811468938 6.704366054 7.723678376 7.607720233 6.90457947 6.837490079 7.437898775 7.608959838 6.704647979 117_at 8.172823015 8.350567625 8.216072585 8.052177294 7.940714093 8.122496258 8.246269493 7.597745286 8.809971026 7.299070143 7.808596965 8.390707085 7.653513989 8.680944931 8.050872759 9.242005826 8.253535243 121_at 10.06419511 11.19368787 9.846188898 10.54999199 10.17272162 10.35728397 11.36108085 10.44613907 11.16554125 10.28543507 10.12355559 10.53273535 10.37933453 10.48754106 10.54241919 10.24804257 10.20725888 1255_g_at 6.256425264 6.830607291 5.825010391 6.098156783 6.104971084 5.818458175 6.355994791 6.31131152 7.366573808 5.577411502 4.570793941 5.046955968 6.561945382 5.897955196 5.40272477 5.957542041 6.201036979 1294_at 9.34788666 9.540260108 9.229501062 9.4643475 9.764903421 9.962180073 9.30045006 9.230649257 9.783263485 8.749284991 9.466964913 9.653450466 9.076623297 9.827835105 9.096732184 9.441370378 9.102000375 1316_at 7.444821595 8.381578766 7.438297503 7.262875144 7.231406742 7.732812769 7.752616718 7.766491871 8.276988479 7.533668585 6.982003095 7.828565136 7.119046748 7.649261001 7.651054255 7.510538903 7.366082474 1320_at 4.645388076 4.776598113 6.887012613 7.275902937 6.087877539 6.841132977 5.35934964 4.514507696 7.731723615 7.531067564 3.93247219 6.254824208 7.125020518 4.264763543 6.58014897 6.747268108 4.255204695 1405_i_at 10.45746907 8.927090755 9.917244096 8.238428875 8.957485742 8.461268645 9.401926859 9.717728257 7.906093812 5.908940792 10.97142109 8.168480828 8.337354139 6.86662155 9.56724363 9.489516637 10.39147919 1431_at 6.095264756 6.96721331 6.271370399 7.683606278 5.608434719 7.288548664 6.613085022 6.386992852 7.172060044 6.283570929 6.027862996 5.749611048 6.410620957 6.074076381 6.454061235 6.894934672 5.412199729 1438_at 8.165022563 8.601696109 8.127203872 8.330128005 7.194363227 7.447903155 7.979068416 7.625896467 8.669911995 8.894808527 6.943193024 8.47454858 10.16632365 7.728061128 7.388841509 7.525051872 7.655379957 1487_at 8.717167566 8.948921171 8.482996756 9.224306979 9.006381141 9.004412187 8.89076478 8.95289573 9.135284813 8.730470712 8.910346798 8.815229729 8.880683672 8.460717188 8.636235029 8.979530875 8.45864663 1494_f_at 8.569307104 9.272563796 8.099782465 8.482816224 8.728872241 8.893912706 9.24799404 8.36195164 9.190847721 8.682856425 7.89146507 12.02424423 8.988280669 8.810714569 8.354246959 8.549493485 8.240601365 1598_g_at 11.55719086 11.63133551 11.46310591 11.22502937 11.16871228 11.6159067 11.18760975 10.91536891 12.13072975 11.3505339 11.15684203 11.29458341 11.46845278 10.94348537 11.48669348 12.11094884 11.55747693 160020_at 9.985305832 10.3690069 10.55363004 9.652222774 9.014218196 9.88234234 10.49070862 9.871292777 10.01205992 9.537328536 8.667695292 9.547152914 9.949466166 10.10975472 10.12075296 10.41474576 9.828687732 1729_at 10.36136675 9.817158399 9.832590412 9.028762922 8.621520517 8.93287561 8.083130024 8.679599605 9.290645997 8.960094561 9.127580834 9.846010315 8.903352147 9.109550293 8.828138425 8.705080643 8.263143078 177_at 6.692846164 7.789500294 7.379171629 6.805409654 6.915594737 7.092665506 7.144847932 5.378081482 7.822745778 7.184686193 6.954013623 6.72866045 7.372252198 6.647955518 7.887163057 7.844297173 6.518292095 1773_at 7.427238548 7.202968595 7.614420308 8.008938758 7.290033624 7.542243543 6.537122301 6.967084261 6.776254109 8.008170278 6.877636117 7.119945885 8.024472104 7.716287778 7.16699594 7.433050044 7.651089965 179_at 9.267843565 9.852632546 9.480728984 10.11332621 9.281188401 9.522899304 9.763622175 9.384980124 10.41921144 9.687657195 9.227467149 9.72530832 9.839523301 9.714825974 9.873303264 9.488571197 9.638988643

如果您需要我的代码,请告诉我。 谢谢

3 个答案:

答案 0 :(得分:0)

我认为问题在于您的输入文件。

您的错误告诉您"GSM119944"不是double的格式,因为它有字母字符。

当您尝试将此字符串转换为double时,会在此行上抛出错误。

Exprs_Val[i-1] = Double.parseDouble(splitarray[i]);

如果无法修复输入,则可以使用try-catch块来处理此无效输入。这可能看起来像这样,但你想在错误场景中做什么取决于你。

for (int i = 0; i < length; i++) {
    System.out.println (splitarray[i]);
    try {
        Exprs_Val[i] = Double.parseDouble(splitarray[i]);
    } catch(NumberFormatException e) {
        // This entry cannot be parsed into a double, insert a value to indicate an error
        Exprs_Val[i] = Double.MAX_VALUE;
    }
}

答案 1 :(得分:0)

您的代码存在一些问题:

1:

您正在迭代超过数组的长度。当ArrayOutOfBoundsException达到值i

时,这会抛出length
int length = splitarray.length;
double[] Exprs_Val=new double[length] ;
for (int i = 1; i< length+1; i++) //"i" will reach value == length, which is out of bounds
{ 
    System.out.println (splitarray[i]);
    Exprs_Val[i-1]=Double.parseDouble(splitarray[i]);
}

2:

“GSM119944”不是有效号码。是什么让你认为你将能够解析为双?你的意思是在“GSM *”之后提取部分吗?

答案 2 :(得分:0)

您的输入是什么样的。当您将数组元素解析为double时,它会引发错误,因为您的String元素包含字母字符。