0

This is for a project for student enrolments. When running the program, I want there to be a menu for the user to input a number from 1-3 which each correspond to a different option. This is the following code I wrote for that menu:

import java.util.*;
import java.io.*;

public class InputMenu 
{
public void display_menu() 
{
System.out.println("1) Enroll a student\n2) View enrollments\n3) To exit program");
System.out.print("Selection: ");
}

public void question()
{

Scanner q = new Scanner(System.in);
   
switch (q.nextInt()) 
{
    case 0:
    System.out.println ("Thank you and goodbye.");
    break;

    case 9:
    System.out.println ("Please proceed.");
    new InputMenu();
    break;

    default:
    System.err.println ( "Unrecognized option" );
    break;
}
}

public InputMenu() 
{
Scanner in = new Scanner(System.in);
    display_menu();

switch (in.nextInt()) 
{
    case 1:
    System.out.println ( "You picked enroll a new student" );
    question();
    break;

    case 2:
    System.out.println ( "You picked view past enrollments" );
    question();
    Scanner = new Scanner(new File("students.txt")); 
    break;

    case 3:
    System.out.println ( "You picked exit program" );
    question();
    break;

    default:
    System.err.println ( "Unrecognized option" );
    break;
}
}

public static void main (String[]args) 
{
new InputMenu();
}
}

and then there is a file 2 called "University.java". It is a large file sorry about that so I cannot post the whole code. This file 2 was given as part of the project instructions. File 2 is the rest of the program runs (collects user inputed student name, address, unit enrolment etc.).

Anyway I want to run this code ^ provided, and when the user inputs the number '1', I want to then run file 2 called "University.java". Is there a way I can do this, or should I try to work out a way to add this code ^ directly within file 2 (not sure how to do that or were to start due to file 2 having different methods and classes). Thanks, and sorry I am a beginner.

  • Take a look at the Javadoc showing exactly how you can launch another java application or any executable file: https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html – sorifiend Jun 17 '22 at 05:40
  • 1
    Does this answer your question? [Java, run another application in foreground](https://stackoverflow.com/questions/11983803/java-run-another-application-in-foreground) see also [Executing another application from Java](https://stackoverflow.com/questions/3468987/executing-another-application-from-java) – sorifiend Jun 17 '22 at 05:42

0 Answers0