0

I am working on a Java Menu-driven program that uses multiple static methods and a menu program running in Main. All menu options run fine except for the first.

The first menu item, PopulateTables(), runs fine outside of the while loop as in the example below:

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

    }*/
}

Here is the static method:

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

If I change Main so that the PopulateTables() method is inside the menu while loop, it looks like it gets into an infinite loop or just doesn't run. As you can see, this is practice and I am a relative novice in Java. Thanks.

  • 1
    You're doing way too much in a single method, you should break up your logic into multiple smaller methods and test each one separately. Your current method is displaying things, reading files, accessing a database. Each of these functions can be broken into its own method. A method should have a single well-defined purpose just like a class. Also you should take a look at [Java Naming Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) because methods should not begin with a capital letter. – D.B. Oct 28 '17 at 16:39
  • Cold you please minimize the code? It seems like its a bit to much – Moritz Schmidt Oct 28 '17 at 16:44
  • Regarding the problem of " infinite loop or just doesn't run" which is it? You need to add logging of some kind (either put in some `System.out.println` or use a logging solution like log4j2, logback, etc) and/or use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) with breakpoints. – D.B. Oct 28 '17 at 16:44

0 Answers0