0

I want to create a program that can do all the stuff from another code, depending on user input. Something like this:

import java.util.Scanner;
public class Main_Programm1 {
    public static void main(String args[]) {
        String something = "something";
        String something2 = "something2";
        Scanner userInput = new Scanner(System.in);
        String action = userInput.next();
        if (action.equals(something)) {
            //here i want to execute all the code from class Main_Programm2
        } else if (action.equals(something2)) {
            //here i want to execute all the code from class Main_Programm3 and so on
        }
    }
}

How do i do it?

3
  • Read about Oops Concept.Constructor,Method calling etc Commented Jun 28, 2015 at 9:19
  • 1
    just call the method you need: Main_Program2.method();... Commented Jun 28, 2015 at 9:20
  • Import the both classes and call their main-method like Main_Programm2.main(args); This should work. Commented Jun 28, 2015 at 9:25

1 Answer 1

1

Actually, you've got it all done, only creates the Objects that you need ;-)

import java.util.Scanner;
// imports classes;

public class Main_Programm1 
{
  public static void main(String args[]) 
  {
    String something = "something";
    String something2 = "something2";
    Main_Programm main_prog;
    Main_Programm2 main_prog2;

    Scanner userInput = new Scanner(System.in);
    String action = userInput.next();
    if (action.equals(something)) 
    {
      main_prog = new Main_Programm();
      //.....
    } 
    else if (action.equals(something2)) 
    {
      main_prog2 = new Main_Programm2();
      //.....
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.