0

I have a program where, if the user enters "1" then the "Assignment01" class will be called and if the user enters "14" then the "Assignment14" class will be called and so on.

Currently, I am using Switch Case Statement to call a function according to the user's number input like shown on the code. But what if there were 100 of classes which needs to be called per the user's input?

It will be really frustrating to have to create a case for calling every single class. So, is there another way to make it short and efficient?

For example:

// Assume user inputs "10" so 'userInput = 10'
String classVariable = "Assignment" + userInput + ".main(args)";

classVariable; // Result in like 'Assignment10.main(args);'

Unfortunately, this can't be done as that is a String but is there a similar way like to define that String into a class so it can be called like that?

EDIT: All I want is that when the user enters integer like say "30" then the class "Assignment30" should be called which can be done like "Assignment30.main(args);" through a switch case statement but I can't create every single case.

It would be better if I could put like say "Assignment+userInput+.main(args);" or something.

CODE:-

switch (userInput)
{

    default:
        AssignmentMain.errorLevel();
        break;

    case 1:
        System.out.println();
        Assignment01.main(args);
        break;


    case 2:
        System.out.println();
        Assignment02.main(args);
        break;

    case 3:
        System.out.println();
        Assignment03.main(args);
        break;

    case 4:
        System.out.println();
        Assignment04.main(args);
        break;

    case 5:
        System.out.println();
        Assignment05.main(args);
        break;

}
RedAISkye
  • 37
  • 9
  • 2
    This can be done with Java's Reflection apis. – StvnBrkdll Aug 14 '17 at 14:47
  • 2
    @mangotang: This *shouldn't* be done with reflection. It sounds like the OP should instead build out an application which has more predictable flow to it as opposed to relying on strings to navigate to critical points in their app. – Makoto Aug 14 '17 at 14:48
  • To put a finer point to it, this sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you tell us what you're actually trying to accomplish? We write complex applications every day that span multiple classes and methods, and doing this by Strings and reflection isn't the immediate thing we turn to. – Makoto Aug 14 '17 at 14:49
  • 1
    `Class.forName("YOUR_CLASS_NAME")` – Abdullah Khan Aug 14 '17 at 14:50
  • 1
    Your question is an [X/Y Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Tell us what problem you are trying to solve instead of asking for help with the solution, which does not appear to be the correct approach. –  Aug 14 '17 at 14:53
  • agree with @Makoto, seems try to resolve error in OO concept (or no concept at all) – Jacek Cz Aug 14 '17 at 14:53
  • 1
    explaining why this is a wrong approach and OO design and analysis is way **too broad** –  Aug 14 '17 at 14:53
  • It *could* be done, but most likely: bad idea. Reflection is a pretty advanced thing, but it boils down to: you **only** use it if you **absolutely** have no option to solve your problem. The whole point of java is to be statically compiled - and if you think you can write better code without that ... simply use a dynamic language ;-) – GhostCat Aug 14 '17 at 14:57
  • No, My question is not an X/Y Problem. I have already stated my program. Let me mention it here: "I have a program where, if the user enters "1" then the "Assignment01" class will be called and if the user enters "14" then the "Assignment14" class will be called and so on." – RedAISkye Aug 14 '17 at 15:01
  • The reason we're saying that it's "XY" is that you don't normally invoke methods based on string parameters. It sounds like to us that you want [the Strategy pattern](https://en.wikipedia.org/wiki/Strategy_pattern) as opposed to reflection. – Makoto Aug 14 '17 at 15:04
  • Please read the Edit. – RedAISkye Aug 14 '17 at 15:09
  • 1
    It is conceivable that the OP is a relatively new programmer, and is interested in exploring Java's capabilities, including using Java's reflection apis. The OP didn't ask for assistance on OO design, he asked for assistance on a solution to a specific problem. It seems reasonable to provide the information that the OP has requested. Certainly there other solutions other than reflection, but reflection is a possible solution. My bet is that if the "whole point of Java is to be statically compiled" the designers of java wouldn't have included a reflection api. – StvnBrkdll Aug 14 '17 at 15:11
  • @mangotang And still. When you are **new** to a language, then spend your time learning on those 25% of the language that you need for 95% of all the tasks a newbie can dream of. Instead of asking for something that is A) a really advanced topic B) not helpful for 95% of all use cases. – GhostCat Aug 15 '17 at 11:00
  • @GhostCat As I said, It will be really frustrating to have to create each an every case for calling every single class when there will be like 100 of classes. So, I just wanted to ask experienced users if they happen to know of another way for a similar method which is short and efficient. – RedAISkye Aug 15 '17 at 14:51
  • @RedEyedRocker I understand. But please believe an experienced user: "you do not know the true meaning of the word *frustration* when you have *not* used reflection code yet". Meaning: it is super easy to get reflection wrong. And you only notice at runtime. When a method is not found. When the way how you put together parameters was wrong ... and even when **one** part of your program passes - you can't be sure that other things are working - unless you tested all of it. – GhostCat Aug 15 '17 at 14:55
  • @GhostCat Alright but isn't there another way other than the Reflection method? – RedAISkye Aug 15 '17 at 15:49

0 Answers0