OK. You need to think about making your class more modular. A good rule of thumb is that you want each method to do ONE thing. Its difficult to get used to if you have a background in scripting but it does make breaking down problems like this a /lot/ easier.
So I had a crack at rewriting your code. I made a Command class that implements the Runnable interface. That means that you can create a Command object that can run itself inside a Thread. Back to the Parsing class and I next wrote an asStrArr method which splits a String on a specified String and returns a String Array. Then I wrote two run methods one for running as a Thread and one for running the commands in series. Finally I needed a doCmd method and a main method to glue it all together.
Hope this helps
Code:
import java.io.*;
import java.util.*;
public class Parsing {
// Main method
public static void main(String args[]) throws Exception {
Parsing p = new Parsing();
// you could do this in a loop if you wanted . . .
p.doCmd("All&Your&Base");
p.doCmd("Are;Belong;To us");
}
// Check for presence of &s and ;s call command
// running methods accordingly. Called once per
// line of commands
public void doCmd(String commandline) {
// Check we don't have both ; and & in same line
if (commandline.indexOf("&")>-1 && commandline.indexOf(";")>-1) {
System.err.println("Error. You can't use &s and ;s in same line");
System.exit(1);
}
else if (commandline.indexOf("&")>-1) {
runParallel(asStrArray(commandline,"&"));
}
else if (commandline.indexOf(";")>-1) {
runSeries(asStrArray(commandline,";"));
}
}
// private utility method to split commandline on a given character
private String[] asStrArray(String commandline, String splitchar) {
return commandline.split(splitchar);
}
// runs commands in parrallel - new Thread object for each
// command
public void runParallel(String[] commands) {
for (int i=0;i<commands.length;i++) {
Command c = new Command(commands[i]);
new Thread(c).start();
}
}
// runs commands in series
public void runSeries(String[] commands) {
for (int i=0;i<commands.length;i++) {
new Command(commands[i]).run();
}
}
}
// A class for illustrating how to implement Runnable,
// and be, therefore, thread-able
class Command implements Runnable {
private String command;
public Command(String cmd) {
command = cmd;
}
// Needs this to implement Runnable. But also used
// when we're running in series.
public void run() {
System.out.println(command);
}
}
Sample output:
Code:
charlie@charlie:~$ java Parsing
All
Your
Are
Belong
To us
Base
charlie@charlie:~$ java Parsing
All
Your
Base
Are
Belong
To us
charlie@charlie:~$
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock:
http://charlieharvey.org.uk