Search code examples
javamenunestednested-loopssubmenu

Can't get a menu to function properly that I created within a nested loop


I'm trying to create program that requires the use of a lot of menus (as a beginner, this stuff is really starting to overwhelm me but it's for a project for my class). This is what I have so far:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class GroupUs {

public static void main(String[] args) {


    String fileName = "CompConClass.txt"; //File includes class roster  

    System.out.println("Hello, would you like to access the class that you have on file or would you like to create a new class?");

    int choice = mainMenu();
    int choice2 = subMenu();

    while (choice != 0) {
        if (choice == 1) {  

            subMenu(); //calls subMenu method

        if (choice2 == 1 ) {

            try {
                BufferedReader br = new BufferedReader(new FileReader(fileName));

                String line = null; //create a line variable 
                while ((line = br.readLine()) != null) { //this will read the file line by line

                    System.out.println(line); //displays every line
                }

              } catch (FileNotFoundException ex) {
                  ex.printStackTrace();
              } catch (IOException ex) {
                  ex.printStackTrace();
              }


        }


        } else if (choice == 2) {
            System.out.println("test");
        }

        choice = mainMenu();

        }

    }        

    public static int mainMenu() {
        Scanner scan = new Scanner(System.in);  // Reading from System.in
        System.out.println( "Press 0 to quit.\n"
                + "Press 1 to access the class that you have on file.\n"
                + "Press 2 to create a new class.\n"
                );
        return scan.nextInt(); // Scans the next token of the input as an int.
    }

    public static int subMenu() {
        Scanner scan = new Scanner(System.in);  // This method will give the teacher the ability to view the roster from the file or add students on to that file
        System.out.println( "What would you like to do to the class on file?\n"
                + "Press 1 to view the students.\n"
                + "Press 2 to add another student.\n"
                + "Press 3 to remove a student."
                );
        return scan.nextInt(); // Scans the next token of the input as an int.
    }      

}

Specifically, I am having trouble at this part of the code

  if (choice == 1) {  

        subMenu(); //calls subMenu method

    if (choice2 == 1 ) {

        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = null; //create a line variable 
            while ((line = br.readLine()) != null) { //this will read the file line by line

                System.out.println(line); //displays every line
            }

          } catch (FileNotFoundException ex) {
              ex.printStackTrace();
          } catch (IOException ex) {
              ex.printStackTrace();
          }

What's happening is that the program initially starts off good by presenting the user with the first mainMenu method that I created. When I input the number 1 to open up my subMenu method, that works correctly as well. However, when I press 1 again (this time to display the roster that is on the file) in side of the subMenu,it just prints out the subMenu again. I press 1 again AND THEN it shows me the roster as desired. I can't figure out why I can't get it to display on the first time around.


Solution

  • int choice2 = subMenu();
    
    while (choice != 0) {
        if (choice == 1) {  
    
            subMenu(); //calls subMenu method
    

    You are calling the subMenu() method twice, which is why it is being run twice.