I need a little help with this program
It is an array problem.
I have the program almost finished but having a problem with it compiling at
this point. I followed everything that is has in our book just changed the
names where I need it to I wanted to get it to work before I added to it I
am going to post both the driver and class can someone help please??
I am also going to post the error that I get.
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at PlayersList.<init>(PlayersList.java:27)
at BaseballGUI.main(BaseballGUI.java:30)
this is the class part of it.
import java.util.Scanner;
public class PlayersList
{
int[][] table;
String[] rowNames;
String[] colNames;
public PlayersList()
{
table = new int [0][0];
rowNames = new String[0];
colNames = new String[0];
}
public PlayersList(String[] rowLabels, String[] colLabels, Scanner inData)
{
rowNames = rowLabels;
colNames = colLabels;
table = new int[rowNames.length][colNames.length];
int row;
int column;
while (inData.hasNext())
{
row = inData.nextInt() - 1;
column = inData.nextInt() -1;
table[row][column]++;
}
}
public String toString()
{
String str = " ";
for (int index = 0; index < table.length; index++)
str = str + " " + colNames[index];
str = str + "\n";
for (int index = 0; index < table.length; index++)
{
str = str + rowNames[index] + " ";
for (int index2 = 0; index2 < table[index].length; index2++)
str = str + " " + table[index][index2];
str = str + "\n";
}
return str;
}
public String PlayerBatsLorRhandedTotals()
{
String str = "";
int sum;
for (int index = 0; index < rowNames.length; index++)
{
sum = 0;
str = str + rowNames[index] + " " + sum + "\n";
}
return str;
}
}
this is the BB GUI
import java.util.Scanner;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class BaseballGUI
{
public static void main(String[] args) throws IOException
{
Scanner inFile;
JFileChooser fileChooser = new JFileChooser();
// Get the current working directory for scores.
String workDir = System.getProperty("user.dir");
// Set the chooser to open in our current directory
fileChooser.setCurrentDirectory(new File(workDir));
// Set the file from the user
fileChooser.setDialogTitle("Batting Averages");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
inFile = new Scanner(fileChooser.getSelectedFile());
String[] rowLabels = {"Hits", "Walks", "Outs", "Player bats L or R"};
String inString = JOptionPane.showInputDialog("Number Players: ");
int players = new Scanner(inString).nextInt();
String[] colLabels = new String[players];
for (int index = 0; index < players; index++)
colLabels[index] = JOptionPane.showInputDialog("Player's Number "+ (index +
1));
// create the table for calculation of the average of left or right handed
batters.
PlayersList PlayersList = new PlayersList(rowLabels, colLabels, inFile);
//prepare to output results in a frame
JFrame out = new JFrame();
out.setSize(400, 600);
Container outPane = out.getContentPane();
outPane.setLayout(new FlowLayout());
JTextArea summary = new JTextArea("\nResults of Left or Right Handed
Batters\n\n" + PlayersList);
summary.setFont(new Font("Courier", Font.PLAIN, 10));
outPane.add(summary);
outPane.add(new JTextArea("\nAverage Lefthanded or right handed:\n\n" +
PlayersList.PlayerBatsLorRhandedTotals()));
out.setVisible(true);
inFile.close();
}
}
}