Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-03-2011, 05:03 PM   PM User | #1
Shyamz1
New to the CF scene

 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Shyamz1 is an unknown quantity at this point
Delete a field program

Hi Guys how are you all?

I'm trying to create a program which takes its input from standard input or a named file and puts its output on standard output or in another named file. I have the following code so far, any suggestions on what i'm doing wrong.

Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;


public class DeleteField 
{
  public static void main(String args[]) 
  {
    // Divide the line into fields using tab as a delimiter
      String[] fields = inputLine.split("\t");
      String editedLine = "";
      if (fields.length < fieldToDelete)
        editedLine = inputLine;
      else
      {
        // We build the new line in parts
	// Add the fields before the one to be deleted.
	for (int index = 0; index < fieldToDelete - 1; index++)
	  if (editedLine.equals("")) editedLine = fields[index];
	  else                       editedLine += "\t" + fields[index];
	// Add the fields after the one to be deleted.
	for (int index = fieldToDelete; index < fields.length; index++)
	  if (editedLine.equals("")) editedLine = fields[index];
	  else			     editedLine += "\t" + fields[index];
      } // else
    
    try 
    {
      int delete = Integer.parseInt(args[0]); 
      // parsing delete
      BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
      PrintStream printStream = new PrintStream(System.out);
      String string = new String();
      while ((string = bufferRead.readLine()) != null) 
      { 
        // reads the line
        String fields[] = string.split("\t"); 
	  // split the line
          for(int i=0;i < fields.length; i++) 
	  {
            if ((i+1) != delete) 
	    { 
	      // if first number equals deleteField number avoid printing, otherwise, print
              printStream.print(fields[i] + "\t");
            }
          }

          printStream.println();
	  
      } // while
   } // try
   
   catch (IOException exception) 
   {
     System.out.println("Error!" + exception.getMessage());
   } // catch
   
   
 } // main
} // class DeleteField
Any suggestions? I dont know how to declare variables whether they should be public or whatever. It's really annoying me now, and I need some help with it otherwise i'll fail first year

Kind regards

Shyam
Shyamz1 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:44 PM.


Advertisement
Log in to turn off these ads.