Produce an output report that contains the player name, FGM, FGA, and the field goal percentage i.e. FGP

StackOverflow https://stackoverflow.com/questions/21837659

  •  12-10-2022
  •  | 
  •  

Question

I have got my code to compile, but it is coming up with a runtime error saying: java.util.InputMismatchException:null(in.java.util.Scanner)

Here is my following code:

import java.io.*;
import java.util.Scanner;
public class kstatebball
{
    public static void main(String [] args)
    throws FileNotFoundException, IOException
    {
        Scanner inFile = new Scanner(new File("data1.txt"));
        int  count = 1;
        int fgm[] = new int [100];
        int fga[] = new int [100];
        double fgp = 0;
        int read,maxFGP,minFGP;

        read = readArray(fgm,fga,count,inFile);
        maxFGP = maxFGP(fgm,fga,count);
        minFGP = minFGP(fgm,fga,count);


        System.out.print("Player        FGM     FGA     FGP%\n");
        System.out.printf("%15d         %3d     %3d     %2.1f",count,fgm,fga,fgp);
        System.out.printf("The player with the highest fieldgoal percantage is: %3d",fgp);
        System.out.printf("The player with the lowest field goal percantage is : %3d",fgp);

        inFile.close();      
    }

    static int readArray(int [] fgm, int [] fga, int count, Scanner inFile)
    {
        double temp = inFile.nextDouble();
        int k;
        int v;
        v = inFile.nextInt();
        for(k = 0; (v!=-999) && (k<count); k++)
        {
            fgm[k] = v;
            v=inFile.nextInt();
        }
        return k;
    }

    public static int maxFGP(int[] fgm, int[] fga, int count)
    {
        for(int i = 0; i < 13; i++)
        {
            if(fgm[i] < fgm[count])
                count = i;            
        }
        return count;
    }

    public static int minFGP(int[]fgm, int[]fga, int count)
    {

        for(int i = 0; i > 13; i++)
        {
            if(fgm[i] > fgm[count])
                count = i;            
        }
        return count;
    }        
}

here is my data file I am reading in:
Marcus Foster    123    288
Thomas Gipson    102    178
Shane Southwell    88    224
Will Spradling    58    144
Wesley Iwundu    53    111
Nino Williams    49    96
Nigel Johnson    28    80
Jevon Thomas    15    58
D.J. Johnson    34    68
Omari Lawrence    27    65
Shawn Meyer 2    4
Ryan Schultz    2    9
Jack Karapetyan    1    4
Brian Rohleder    1    2
-999

I am trying to get my output to look as the following:

    Marcus Foster    123    288        42%
    Thomas Gipson    102    178        57%
    Shane Southwell    88    224       39%      
    Will Spradling    58    144        40%
    Wesley Iwundu    53    111         47%
    Nino Williams    49    96          51%
    Nigel Johnson    28    80          35%
    Jevon Thomas    15    58           25%
    D.J. Johnson    34    68           50%
    Omari Lawrence    27    65         41%
    Shawn Meyer 2    4                 50%
    Ryan Schultz    2    9             22%
    Jack Karapetyan    1    4          25%
    Brian Rohleder    1    2           50%

The player with the highest field goal percentage is Thomas Gibson with 57%.

The player with the lowest field goal percentage is Ryan Schultz with 22%

Was it helpful?

Solution

An error of java.util.InputMismatchException is thrown by the java.util.Scanner class when you call a method, like nextDouble or nextInt(), when the data read doesn't match the type you've told it to read. See http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()

Based on your input file, you are incorrectly attempting to read Strings as a double.

static int readArray(int [] fgm, int [] fga, int count, Scanner inFile)
{
    double temp = inFile.nextDouble();
    int k;
    int v;
    v = inFile.nextInt();
    for(k = 0; (v!=-999) && (k<count); k++)
    {
        fgm[k] = v;
        v=inFile.nextInt();
    }
    return k;
}

OTHER TIPS

Without much going into your code, I think you try to read the players name as a double value. It should by a String instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top