Reputation: 443
I have 100 files named "1.exe","2.exe","3.exe",...,"100.exe" I want to take input from user as 1,2,3,... or 100 and run the corresponding exe file. For example if user inputs 45 , I will run file "45.exe" I don't want to use ifs or switches. Can anyone please help me.
Upvotes: 4
Views: 146
Reputation: 237
Runtime.getRuntime().exec( input + ".exe" ).waitFor();
will work if you want to to wait for it.
Upvotes: 2
Reputation: 15099
My java is a little rusty, forgive me, but this should give you the idea:
Form a string by doing something like this:
String number = "45"; // or whatever user inputs
String suffix = ".exe";
String file = number + suffix;
Then once you have the string, use it to run a process:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(file);
And when you're done with the process, destroy it:
p.destroy();
Upvotes: 1
Reputation: 3780
If they're in the same folder, you can use also use java.nio.file.Paths to resolve them with:
Runtime.getRuntime().exec(Paths.get(getNumberInput() + ".exe").toString());
Upvotes: 2
Reputation: 16403
If the input is always equal to the filename of your exes you can do it with:
if( isInputNumberBetween1And100() )
Runtime.getRuntime().exec( input + ".exe" );
Upvotes: 13