How do I assign values to multiple varibles from one line of string. For example.
String time = sc.next(); // the amount of time to delay from shutdown
String timeframe = sc.next(); // time frame : seconds, minutes, ect
If this would be for a shutdown code, and the user enters "10 mins", how would I assign the variable 10 to an integer (of course after I parse it), and use the time frame (along with some math) to create the delay to the shutdown.
The end goal, for this part of the program, is to give the user the ability to enter one clean command "shutdown 10 mins", and to have windows schedule a shutdown 10 minutes from now.
Currently, the user would have to do : "shutdown" [ENTER] "10" [ENTER] "mins" [ENTER].
The rest of the program is fine, only this gives me the problem of irregularity.
Complete code for class :
public void Start() {
Scanner sc = new Scanner(System.in);
int xtime = 0;
boolean goodTime = false, goodTimeFrame = false;
String time = sc.next();
String timeframe = sc.next();
try {
xtime = Integer.parseInt(time);
if(xtime<0) { System.out.println("time cannot be less than 0..."); }
else if(xtime>=0) { goodTime = true; }
} catch(Exception e){ System.out.println("invalid input..."); }
try {
switch(timeframe.toLowerCase()) {
case "secs":
xtime = xtime*1000;
goodTimeFrame = true;
break;
case "mins":
xtime = xtime*60000;
goodTimeFrame = true;
break;
case "hrs":
xtime = xtime*3600000;
goodTimeFrame = true;
break;
default:
System.out.println("invalid timeframe, use: secs, mins, or hrs...");
break;
}
} catch(Exception e){ System.out.println("cannot convert to valid time..."); }
if(goodTime && goodTimeFrame) {
try {
Runtime.getRuntime().exec("Elevate.exe shutdown -s -t "+xtime);
} catch(Exception e){ System.out.println("error ordering shutdown command..."); }
}
else { System.out.println("syntex example : shutdown time timeframe"
+ "\nshutdown 10 mins"); }
}