我正在开发一个Java Menu驱动的程序,该程序使用多个静态方法和在Main中运行的菜单程序。除第一个菜单选项外,所有菜单选项都运行良
第一个菜单项PopulateTables()在while循环之外运行正常,如下例所示:
public class Menu {
static Connection con = JDBCConnection.connect();
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int answer = 0;
PopulateTables();
/*while (answer != 6) {
Menu();
System.out.println("Which would you like to do?");
answer = scan.nextInt();
scan.nextLine();
if (answer == 1) {
PopulateTables();
}
if (answer == 2) {
TeamsWins();
}
if (answer == 3) {
NumberTeamWins();
}
if (answer == 4) {
YearWorldSeries();
}
if (answer == 5) {
//AddNewYear();
}
if (answer == 6) {
System.out.println("See you later!");
System.exit(0);
}
}*/
}
这是静态方法:
public static void PopulateTables() {
List<Team> team = new ArrayList<>();
List<Game> game = new ArrayList<>();
Frame f = new Frame();
FileDialog foBox = new FileDialog(f, "Reading text file", FileDialog.LOAD);
foBox.setVisible(true);
String foName = foBox.getFile();
String dirPath = foBox.getDirectory();
// create a file instance for the absolute path
File inFile = new File(dirPath + foName);
BufferedReader in = null;
try {
// create a BufferedReader to use to read in the file
in = new BufferedReader(new FileReader(inFile));
// read in the first entire line from the file
String line = in.readLine();
int year = 1903;
int teamID = 1;
// continue until the line is null (ie you are at the end of the file)
while (line != null) {
StringTokenizer t = new StringTokenizer(line, ",");
String teamOneName = t.nextToken().trim();
String teamOneLeague = t.nextToken().trim();
String teamTwoName = t.nextToken().trim();
String teamTwoLeague = null;
if (teamOneLeague.equalsIgnoreCase("AL")) {
teamTwoLeague = "NL";
} else {
teamTwoLeague = "AL";
}
Team firstTeam = new Team(teamOneName, teamOneLeague);
Team secondTeam = new Team(teamTwoName, teamTwoLeague);
boolean firstTrue = false;
boolean secondTrue = false;
for (int i = 0; i < team.size(); i++) {
if (firstTeam.getTeamName().equalsIgnoreCase(team.get(i).getTeamName())) {
firstTrue = true;
}
}
if (firstTrue == false) {
firstTeam.setTeamID(teamID);
team.add(firstTeam);
teamID++;
} else {
for (int i = 0; i < team.size(); i++) {
if (team.get(i).getTeamName().equals(teamOneName)) {
firstTeam.setTeamID(team.get(i).getTeamID());
}
}
}
for (int i = 0; i < team.size(); i++) {
if (secondTeam.getTeamName().equalsIgnoreCase(team.get(i).getTeamName())) {
secondTrue = true;
}
}
if (secondTrue == false) {
secondTeam.setTeamID(teamID);
team.add(secondTeam);
teamID++;
} else {
for (int i = 0; i < team.size(); i++) {
if (team.get(i).getTeamName().equals(teamTwoName)) {
secondTeam.setTeamID(team.get(i).getTeamID());
}
}
}
Game newGame = new Game(year, firstTeam, secondTeam);
game.add(newGame);
if (year == 1903 || year == 1993) {
year = year + 2;
} else {
year++;
}
// read in the next line
line = in.readLine();
}
} // catch any IOException that occurs
catch (IOException io) {
System.out.println("An IO Exception occurred");
io.printStackTrace();
} finally // finally always runs no matter what so close the file here!
{
// close the file. Java is neurotic - it worried "but what if it is already closed?" so needs another try/catch
try {
in.close();
} catch (Exception e) {
} // note the {} - means "do nothing". I wanted it closed anyway.
}
Statement stmt = null;
try {
for (int i = 0; i < team.size(); i++) {
Team teamInserted = team.get(i);
String teamName = teamInserted.getTeamName();
String league = teamInserted.getLeague();
String teamID = Integer.toString(teamInserted.getTeamID());
stmt = con.createStatement();
stmt.executeUpdate("insert into team "
+ "values(" + teamID + ", '" + teamName + "', '" + league + "')");
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQL Exception");
}
try {
for (int i = 0; i < game.size(); i++) {
Game gameInserted = game.get(i);
String gameYear = Integer.toString(gameInserted.getYearGame());
String winID = Integer.toString(gameInserted.getWinTeam().getTeamID());
String lossID = Integer.toString(gameInserted.getLossTeam().getTeamID());
stmt = con.createStatement();
stmt.executeUpdate("insert into game "
+ "values(" + gameYear + ", " + winID + ", " + lossID + ")");
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQL Exception");
}
finally{
System.exit(0);
}
如果我改变Main以便PopulateTables()方法在循环中的菜单内部,它看起来像进入无限循环或者只是不运行。正如您所看到的,这是练习,我是Java的相对新手。感谢。