Issue
I am having trouble getting my submenu to show up when I select input 3)Select Students in my main menu. Any help fixing this is greatly appreciated, Thank you!
here is the main menu (shows up first when I run the code)
public void menu() throws ClassNotFoundException, IOException {
Scanner sc = new Scanner(System.in);
int input;
do {
System.out.println("1) Populate Students");
System.out.println("2) Load Students from file");
System.out.println("3) Select Students");
System.out.println("4) Show Students");
System.out.println("5) Save Students to file");
System.out.println("6) Exit");
input = Integer.parseInt(sc.next());
if (input == 1)
{
populateStudents();
} else if (input == 2) {
loadStudents();
} else if (input == 3) {
selectStudents();
} else if (input == 4) {
showStudents();
} else if (input == 5) {
saveStudents();
}
} while (input != 6);
}
This is my sub menu code, I want to be able to select input 3 from the main menu and call this sub menu to then select from.
public void studentSubMenu(int i) throws IOException, ClassNotFoundException
{
System.out.println("Student Menu, select an option");
int input = 0;
while(input != 5)
{
System.out.println("1) Calculate Average");
System.out.println("2) Calculate Weighted Average");
System.out.println("3) Determine Letter Grade");
System.out.println("4) Display Students info");
System.out.println("5) GO BACK");
Scanner sc = new Scanner(System.in);
input=sc.nextInt();
if (input == 1)
{
myStudents[i].calcAvg();
}
if (input == 2)
{
myStudents[i].calcAvg(.7, .3);
}
if (input == 3)
{
myStudents[i].calcLetterGrade();
}
if (input == 4)
{
myStudents[i].displayStudent();
} else if (input == 5) {
menu();
}
}
Solution
Expanding on my comment:
The issue here is that the case you have written for handling the input of 3
in the menu
method calls a function called selectStudents
.
This is not the method containing your sub menu logic. That method you have named studentSubMenu
. This means that if the desired behavior of a user entering 3
is them being shown the sub menu, this won't occur unless studentSubMenu
is either called directly in the else if (input == 3)
block, or by selectStudents
itself.
Your logic in menu
could look something like:
} else if (input == 3) {
int student = selectStudents();
studentSubMenu(student);
}
Answered By - Reece M
Answer Checked By - Willingham (JavaFixing Volunteer)