Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 9th, 2007, 09:35 AM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default using delimiters---i need Heeeeelp!!!

i need to enter commands in one line using delimiter "&" (ampersand) or ";" (semicolon),using one type of delimiter in a line. e.g "cat&cp&rm" or "cat;cp;rm". Both should not be used in one line e.g "cat;cp&rm", when this happens the system exits.
I've tried the code below using Split method but i want to use an IF statement for the above condition. i also want to run a thread for each command if the commands are seperated by "&" and run only one thread for all commands being executed one after another if they're seperated by ";".

Code:
import java.lang.*;
import java.io.*;
import java.util.*;

public class Parsing
{
     public static void main(String args[]) throws Exception
     {
          new Parsing().Split();
     }

     public void Split()
    {
         String command = " ";
     System.out.print("Enter command: " );
     try{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        command = br.readLine();

              String [] temp = null;
              temp = command.split("\\&");

            /*if((temp.equals("&")) || (temp.equals(";")))
            {
           write(temp);
            }
               else
           {
          System.exit(0);
           }*/

      }catch (IOException e){}
    }

  public void write(String []s)
  {
      for (int i = 0 ; i < s.length ; i++)
      {
          System.out.println(s[i]);
      }

  }
}

how should i do that? please help me. even if there's no actual solution, i need your suggestions. Thanks in advance
Reply With Quote
  #2 (permalink)  
Old June 15th, 2007, 06:22 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using delimiters---i need Heeeeelp!!! ssimkhan Pro Java 1 June 15th, 2007 06:55 AM
how to read delimiters debo_sbg XSLT 7 May 11th, 2005 11:20 AM
Export Delimiters CodingMonkey VB Databases Basics 0 August 19th, 2004 12:40 PM
dbgrid export and print...HEEEEELP!!!! killer_kamatis VB How-To 0 August 9th, 2003 11:15 AM



All times are GMT -4. The time now is 12:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.