javainputmismatchexception

Issue with nextFloat in java


I need it to skip the first line of the text file and then the first two columns of the table. So far I have:

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

The problem is that it throws an Input mismatch exception on the nextFloat() method of the scanner, but the value it should be reading is a float. I am unsure of the problem and was looking for someone more knowledgeable than me. Here is the text file i am supposed to be reading from.

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   

Solution

  • You only read 6 columns when using readFloat in a loop, which basically means when you think you are on the next row - you are not, you are at the last element of current row, hence when you skip two more entries you end up trying to parseFloat from the value from column 2 - which is "Q3" and clearly not quite a float. But your input file has 7 columns. I suggest you use your constants (ROWS/COLUMNS) instead of hardcoded numbers, e.g.:

    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 and you can do increment as part of the for() statement, as above.