0

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"); }
    }
0

3 Answers 3

2

When working in Java, the Javadoc is your #1 reference. String has many methods for extracting the contained data. String.split() is your friend:

String line = "shutdown 10 mins";
String[] split = line.split("\\s+"); // Split the String on one or more spaces
String cmd = split[0];
String time = split[1];
String timeframe = split[2];
Sign up to request clarification or add additional context in comments.

Comments

2

Actually this should work the way you wrote it.

When user types shutdown 10 mins[ENTER] you will get sc.next() called 3 times with corresponding values "shutdown", "10" and "mins".

Update: in your case you shouldn't change two times due to unneeded complexity. Just scan whole line at one using '\n' as delimeter and pass values parameters to your second method:

    Scanner sc = new Scanner(System.in).useDelimiter("\n");
    String wholeCommand=sc.next(); // will read "shutdown 10 mins" as one line
    String[] values = wholeCommand.split(" ");
    // check of array length here

    if ("shutdown".equals(values[0])) {
         start(value[1], values[2]); 
    }

1 Comment

Ok so, I currently have to enter "shutdown"[ENTER] "10 mins"[ENTER], since the class shown in this post is separate from the main class. So is there anyway I can enter "shutdown 10 mins" in the main class, pass it over to the second class, then search the string if it contains only either "secs, mins, hrs"?
0

Here is a better solution that I came up with.

String input = sc.nextLine();
String[] temp = input.split(" ");
LinkedList<String> command = new LinkedList<String>(Arrays.asList(temp));

if(command.get(0).equalsIgnoreCase("shutdown") && command.size()==3) {
    Shutdown shutdown = new Shutdown();
    shutdown.Start(command);
}

Now inside the Shutdown.class ...

public void Start(LinkedList<String> command) {
        int xtime = 0;
        boolean goodTime = false, goodTimeFrame = false;

        String time = command.get(1);
        String timeframe = command.get(2);

        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":
                    goodTimeFrame = true;
                    break;
                case "mins":
                    xtime = xtime*60;
                    goodTimeFrame = true;
                    break;
                case "hrs":
                    xtime = xtime*3600;
                    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("cmd /c 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"); }
    }

So we basically created an array, then converted it into a LinkedList. From there we passed the variables over to the shutdown class which did with it as intended. All the user has to do is type the line "shutdown 10 min", and the command will be started.

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.