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