首页 > 解决方案 > java中nextFloat的问题

问题描述

我需要它来跳过文本文件的第一行,然后是表格的前两列。到目前为止,我有:

}public static float [][] read_data(String strFileName){
        float[][] fileread = new float[ROWS][COLUMNS];
        File filedata = new File(strFileName);
        try {
    Scanner fileReader = new Scanner(filedata);


    System.out.println(fileReader.nextLine());


        for(int rowCounter=0;rowCounter<23;) {

            System.out.println(fileReader.next());
            System.out.println(fileReader.next());

        for(int counter = 0;counter<6;) {


            fileread[rowCounter][counter]= fileReader.nextFloat();


            counter++;

        }

        rowCounter++;}
    fileReader.close();
    }catch(FileNotFoundException e) {
        System.out.println("File Not Found!");
    }return fileread;
    }

问题是它会在扫描仪的 nextFloat() 方法上引发输入不匹配异常,但它应该读取的值是浮点数。我不确定这个问题,正在寻找比我更有知识的人。这是我应该从中读取的文本文件。

Federal_Fiscal_Year         
2014    Q2  255.3   350.7   606.0   37.8    62.2    8.3 373.6       
2014    Q3  254.1   355.1   609.2   38.6    61.9    8.2 378.8       
2014    Q4  258.8   370.8   629.7   41.2    65.1    8.2 385.8       
2015    Q1  258.0   373.0   630.9   40.9    65.1    8.2 394.9       
2015    Q2  262.9   388.4   651.3   43.4    68.7    8.3 402.8       
2015    Q3  260.7   390.8   651.6   44.1    68.0    8.2 410.3       
2015    Q4  264.8   404.9   669.7   46.8    71.1    8.1 416.7       
2016    Q1  264.1   406.6   670.7   46.6    71.1    8.1 423.6       
2016    Q2  269.0   421.8   690.8   49.4    75.2    8.2 431.2       
2016    Q3  266.7   423.5   690.3   50.2    74.5    8.0 439.2       
2016    Q4  270.1   436.1   706.2   53.0    77.8    7.9 447.3       
2017    Q1  268.8   436.5   705.3   52.8    77.5    7.9 456.2       
2017    Q2  272.5   449.6   722.2   55.7    81.5    7.9 464.5       
2017    Q3  269.9   450.3   720.2   56.6    80.5    7.8 472.3       
2017    Q4  273.1   462.4   735.5   59.7    83.9    7.6 480.3       
2018    Q1  272.2   463.3   735.5   59.6    83.7    7.6 489.0       
2018    Q2  276.4   476.6   753.0   62.8    87.7    7.6 495.9       
2018    Q3  274.2   477.8   752.1   63.9    86.7    7.4 501.9       
2018    Q4  277.5   489.6   767.1   67.0    89.9    7.1 508.0       
2019    Q1  276.8   490.9   767.7   71.3    89.8    6.9 515.6       
2019    Q2  280.0   503.5   783.5   70.7    93.9    6.6 521.8       
2019    Q3  277.4   504.2   781.6   71.9    92.9    6.3 528.4       
2019    Q4  280.7   516.0   796.7   75.2    96.1    6.1 536.1       
2020    Q1  279.6   516.3   795.8   75.3    95.6    5.9 542.4   

标签: javainputmismatchexception

解决方案


在循环中使用 readFloat 时,您只读取 6 列,这基本上意味着当您认为您在下一行时 - 您不是,您位于当前行的最后一个元素,因此当您跳过另外两个条目时,您最终会尝试从第 2 列的值中解析浮点数 - 这是“Q3”,显然不是一个浮点数。但是您的输入文件有 7 列。我建议您使用常量(ROWS/COLUMNS)而不是硬编码数字,例如:

for (int rowCounter = 0; rowCounter < ROWS; rowCounter++) {
    System.out.println(fileReader.next());
    System.out.println(fileReader.next());

    for (int counter = 0; counter < COLUMNS; counter++) {
        fileread[rowCounter][counter] = fileReader.nextFloat();
    }
}

PS,您可以将增量作为 for() 语句的一部分,如上所述。


推荐阅读