Question

I have a method which uses a switch statement to give the user options to select, once they have selected an option and the code in the case has been executed how do I get back into the main method which offers another menu?

Part of my code:

  static void modifyStudent() {
    System.out.println("Wish student would you like to change?");
    for(int i=0;i<10;i++){
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    System.out.println("1: Change name.");
    ....
    int detailChange = input.nextInt();
    switch (detailChange) {
      case 1:
        String newName = input.next();
        studentNamesArray[studentChoice] = newName;
      break;
      ....
     }


  public static void main(String[] args) {
    while (1 == 1) {
    System.out.println("Please select an option:");
    System.out.println("1: Add a student.");
    ....
    int choice = input.nextInt();

      switch (choice) {
        case 1:
        ....
   }

EDIT (full code requested):

/**
 * User: Colin Shewell
 * Date: 26/11/13
 * Time: 10:32
 */
import java.util.Scanner;
import java.util.Arrays;      //REMOVE THIS!!!
public class StudentMarks {
  static Scanner input = new Scanner(System.in);
  static String[] studentNamesArray = new String[10];
  static int[][] studentMarksArray = new int[10][3];
  static int nameArrayCount, markArrayCount = 0;
  static int markOne, markTwo, markThree;
  static String studentName;

  static void printArrays(){
      System.out.println(Arrays.toString(studentNamesArray));
      for (int index=0;index<10;index++)
      {

          System.out.println(studentMarksArray[index][0]);
          System.out.println(studentMarksArray[index][1]);
          System.out.println(studentMarksArray[index][2]);
      }
  }

  static void addStudent() {
    if (nameArrayCount < 10) {
      System.out.println("Enter the student's name in the following format - surname, forename: ");
      studentName = input.next();
      studentNamesArray[nameArrayCount] = studentName;
      nameArrayCount = nameArrayCount + 1;
    }
    else if (nameArrayCount == 10) {
      System.out.println("******Array is full, please delete a student before adding another.*****");
    }
    if (markArrayCount < 10){
    System.out.println("Enter the first mark: ");
          markOne = input.nextInt();
          System.out.println("Enter the second mark: ");
          markTwo = input.nextInt();
          System.out.println("Enter the third mark: ");
          markThree = input.nextInt();
          studentMarksArray[markArrayCount][0] = markOne;
          studentMarksArray[markArrayCount][1] = markTwo;
          studentMarksArray[markArrayCount][2] = markThree;
          markArrayCount = markArrayCount + 1;
    }
  }

  static void modifyStudent() {
    System.out.println("Wish student would you like to change?");
    for(int i=0;i<10;i++){
      System.out.println(i + ": " + studentNamesArray[i]);
    }
    int studentChoice = input.nextInt();
    System.out.println("1: Change name.");
    System.out.println("2: Change first mark.");
    System.out.println("3: Change second mark.");
    System.out.println("4: Change third mark.");
    System.out.println("5: Change all marks.");
    int detailChange = input.nextInt();
    switch (detailChange) {
      case 1:
        System.out.println("Enter the new student name.");
        String newName = input.next();
        studentNamesArray[studentChoice] = newName;
        return;
      case 2:
        System.out.println("Enter the new mark for mark one.");
        int newMarkOne = input.nextInt();
        studentMarksArray[studentChoice][0] = newMarkOne;
      return;
      case 3:
        //two
      break;
      case 4:
        //three
      break;
      case 5:
        //all
      break;
      default:
        System.exit(0);
      break;
    }
  }

  public static void main(String[] args) {
    while (true) {
      System.out.println("Please select an option:");
      System.out.println("1: Add a student.");
      System.out.println("2: Modify the details of an existing student.");
      System.out.println("3: Delete an existing student.");
      System.out.println("4: Sort in alphabetical order by name.");
      System.out.println("5: Output the student name and corresponding marks in ascending name order.");
      System.out.println("6: Output the student name and corresponding marks in descending name order.");
      System.out.println("7: Display the student with the highest average mark.");
      System.out.println("8: Display the student with the lowest average mark.");
      System.out.println("9: Display the average score of all students recorded.");
      System.out.println("10: Exit.");
      int choice = input.nextInt();

      switch (choice) {
        case 1:
          addStudent();
          System.out.println(Arrays.toString(studentNamesArray));
        break;
        case 2:
          modifyStudent();
        break;
        case 3:
          printArrays();
        break;
       /* case 4:
          sortAlphabetical();
        break;
        case 5:
          outputNameMarksAsc();
        break;
        case 6:
          outputNameMarksDsc();
        break;
        case 7:
          highestStudentAvgMark();
        break;
        case 8:
          lowestStudentAvgMark();
        break;
        case 9:
          displayAvgScore();
        break;         */
        case 10:
          System.exit(0);
        break;
        default:
          System.exit(0);
        break;
      }
    }
  }
}
Was it helpful?

Solution

Just return to the main method. return just exits the method and continues executing code from the line it was called.

  case 1:
    String newName = input.next();
    studentNamesArray[studentChoice] = newName;
    return;  //exits this method
  //break; <-- not needed after a return!

OTHER TIPS

Your code will run in an infinite loop, you can make it run with a conditional flag like this

boolean isRunning = true;
while (isRunning) {
    System.out.println("Please select an option:");
    System.out.println("1: Add a student.");
    ....
    int choice = input.nextInt();

      switch (choice) {
        case 1:
        isRunning = false;
        //your code for case 1
        ....
   }

//rest of the code in main executes now

This will exit the while loop back to the main method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top