New To Java & Need Help With Program I'm Writing - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

New To Java & Need Help With Program I'm Writing

adlb1300adlb1300 Posts: 9Member
Hi everyone. I'm new to Java and programming in general. I'm trying to write a file parsing program that takes a report saved in one text doc and pulls out the need information saving it to another text doc. Basically it removes the unneeded information and puts it in format that is easy to import to say a database. It was working fine until I added in the last piece that process the individual lines of data. The program will now compile in a class doc but nothing is outputted to the output doc. Before this happened I was able to pull other parts that I wanted and have it output to the new file correctly. I'm trying to produce a report that looks like this:

Rundate ASC Loan DelqM - header
6/1/07 036 123456789 Y - data

The file to parse looks like this:

Report: Fake Report for Fake Loans
Report ID: 000001
Run Date: 04/25/2007
----------------------------------------------------------------------
ASC Number Loan ID DelqMessage
033 000101000 N
034 001234875 Y
035 123456789 N

----------------------------------------------------------------------
ASC Number Loan ID DelqMessage
036 741258963 N
037 872563987 N
038 987521455 Y

======================================================================

Fake Report Summary:
ASC Total: 6
Loan Total: 6
Total Delq: 2

The following is the code I have so far:

[code]
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class ParseFile {
public static void main(String[] args) throws IOException {

// Variable Declaration

boolean procOn;
String RunDate = "";
BufferedReader inputStream = null;
PrintWriter outputStream = null;

// Following block of code parses FakeReportToParse.txt and outputs
// needed portions of the report to characteroutput.txt

try {

procOn=false;

inputStream =
new BufferedReader(new FileReader("FakeReportToParse.txt"));
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));

String l;
while ((l = inputStream.readLine()) != null) {

outputStream.println("Rundate ASCNumber Loan DelqMessage");

if (l.indexOf("Run Date:")>=0) {
RunDate = l.substring(10);
outputStream.println(RunDate);
}

// Processing of report data

if(l.indexOf("ASC Number Loan ID") >= 0) {
procOn = true;
}

if(l.indexOf("-----------") >= 0 || l.indexOf("===========") >= 0) {
procOn = false;
}

if(l.indexOf("") >= 0) {
procOn = false;
}

// Output report data to new characteroutput.txt file

if(procOn) {
String ASC = l.substring(0, 4).trim();
String LoanID = l.substring(15, 25).trim();
String DelqM = l.substring(30).trim();

outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);

}
}
}
catch(Exception z){
System.out.println("Error: " + z.toString());
}
}
}
[/code]

Any help would be appreciate as I'm stuck as to what is wrong.

Thanks
«1

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi everyone. I'm new to Java and programming in general. I'm trying
    : to write a file parsing program that takes a report saved in one
    : text doc and pulls out the need information saving it to another
    : text doc. Basically it removes the unneeded information and puts it
    : in format that is easy to import to say a database. It was working
    : fine until I added in the last piece that process the individual
    : lines of data. The program will now compile in a class doc but
    : nothing is outputted to the output doc. Before this happened I was
    : able to pull other parts that I wanted and have it output to the new
    : file correctly. I'm trying to produce a report that looks like this:
    :
    : Rundate ASC Loan DelqM - header
    : 6/1/07 036 123456789 Y - data
    :
    : The file to parse looks like this:
    :
    : Report: Fake Report for Fake Loans
    : Report ID: 000001
    : Run Date: 04/25/2007
    : ---------------------------------------------------------------------
    : -
    : ASC Number Loan ID DelqMessage
    : 033 000101000 N
    : 034 001234875 Y
    : 035 123456789 N
    :
    : ---------------------------------------------------------------------
    : -
    : ASC Number Loan ID DelqMessage
    : 036 741258963 N
    : 037 872563987 N
    : 038 987521455 Y
    :
    : =====================================================================
    : =
    :
    : Fake Report Summary:
    : ASC Total: 6
    : Loan Total: 6
    : Total Delq: 2
    :
    : The following is the code I have so far:
    :
    : [code]:
    : import java.io.FileReader;
    : import java.io.FileWriter;
    : import java.io.BufferedReader;
    : import java.io.PrintWriter;
    : import java.io.IOException;
    :
    : public class ParseFile {
    : public static void main(String[] args) throws IOException {
    :
    : // Variable Declaration
    :
    : boolean procOn;
    : String RunDate = "";
    : BufferedReader inputStream = null;
    : PrintWriter outputStream = null;
    :
    : // Following block of code parses FakeReportToParse.txt and outputs
    : // needed portions of the report to characteroutput.txt
    :
    : try {
    :
    : procOn=false;
    :
    : inputStream =
    : new BufferedReader(new FileReader("FakeReportToParse.txt"));
    : outputStream =
    : new PrintWriter(new FileWriter("characteroutput.txt"));
    :
    : String l;
    : while ((l = inputStream.readLine()) != null) {
    :
    : outputStream.println("Rundate ASCNumber Loan DelqMessage");
    :
    : if (l.indexOf("Run Date:")>=0) {
    : RunDate = l.substring(10);
    : outputStream.println(RunDate);
    : }
    :
    : // Processing of report data
    :
    : if(l.indexOf("ASC Number Loan ID") >= 0) {
    : procOn = true;
    : }
    :
    : if(l.indexOf("-----------") >= 0 || l.indexOf("===========") >= 0) {
    : procOn = false;
    : }
    :
    : if(l.indexOf("") >= 0) {
    : procOn = false;
    : }
    :
    : // Output report data to new characteroutput.txt file
    :
    : if(procOn) {
    : String ASC = l.substring(0, 4).trim();
    : String LoanID = l.substring(15, 25).trim();
    : String DelqM = l.substring(30).trim();
    :
    : outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    :
    : }
    : }
    : }
    : catch(Exception z){
    : System.out.println("Error: " + z.toString());
    : }
    : }
    : }
    : [/code]:
    :
    : Any help would be appreciate as I'm stuck as to what is wrong.
    :
    : Thanks
    :
    You forgot to close() the streams. Only when you close them, will the data be flushed to the storage medium.
  • adlb1300adlb1300 Posts: 9Member
    : You forgot to close() the streams. Only when you close them, will
    : the data be flushed to the storage medium.

    I added the following piece of code within a finally to resolve the closing of the outputstream/inputstream.

    [code]
    finally {
    if (inputStream != null) {
    inputStream.close();
    }
    if (outputStream != null) {
    outputStream.close();
    }
    }
    [/code]

    This has partly resolved my prior issue. I now get part of the expected output as the header info is outputted as well as one instance of the 4/25/07 run date. However, none of the lines of data are being outputted. Any hints as to what I may be doing wrong here.
  • zibadianzibadian Posts: 6,349Member
    : : You forgot to close() the streams. Only when you close them, will
    : : the data be flushed to the storage medium.
    :
    : I added the following piece of code within a finally to resolve the
    : closing of the outputstream/inputstream.
    :
    : [code]:
    : finally {
    : if (inputStream != null) {
    : inputStream.close();
    : }
    : if (outputStream != null) {
    : outputStream.close();
    : }
    : }
    : [/code]:
    :
    : This has partly resolved my prior issue. I now get part of the
    : expected output as the header info is outputted as well as one
    : instance of the 4/25/07 run date. However, none of the lines of data
    : are being outputted. Any hints as to what I may be doing wrong here.
    :
    This part of your code seems to be the problem:
    [code]
    : if(l.indexOf("") >= 0) {
    : procOn = false;
    : }
    [/code]
    This checks if the string l has an empty part in it, which is always true. If you want to check whether 2 strings are equal, then you need to use the String.equals() method.
  • adlb1300adlb1300 Posts: 9Member
    : : : You forgot to close() the streams. Only when you close them, will
    : : : the data be flushed to the storage medium.
    : :
    : : I added the following piece of code within a finally to resolve the
    : : closing of the outputstream/inputstream.
    : :
    : : [code]: :
    : : finally {
    : : if (inputStream != null) {
    : : inputStream.close();
    : : }
    : : if (outputStream != null) {
    : : outputStream.close();
    : : }
    : : }
    : : [/code]: :
    : :
    : : This has partly resolved my prior issue. I now get part of the
    : : expected output as the header info is outputted as well as one
    : : instance of the 4/25/07 run date. However, none of the lines of data
    : : are being outputted. Any hints as to what I may be doing wrong here.
    : :
    : This part of your code seems to be the problem:
    : [code]:
    : : if(l.indexOf("") >= 0) {
    : : procOn = false;
    : : }
    : [/code]:
    : This checks if the string l has an empty part in it, which is always
    : true. If you want to check whether 2 strings are equal, then you
    : need to use the String.equals() method.

    Thanks that code is there to check if the line is blank. I was getting an error message within the procOn part because there is a blank line between the last line of data and the symbols I was using to end it so that the series of ===== is not outputted. What I'm looking to do is turn procOn to false if the entire line is blank. Is there a way to say if line is completely blank do this? Such a certain class, etc I should look into that might resolve for me?
  • zibadianzibadian Posts: 6,349Member
    : : : : You forgot to close() the streams. Only when you close them, will
    : : : : the data be flushed to the storage medium.
    : : :
    : : : I added the following piece of code within a finally to resolve the
    : : : closing of the outputstream/inputstream.
    : : :
    : : : [code]: : :
    : : : finally {
    : : : if (inputStream != null) {
    : : : inputStream.close();
    : : : }
    : : : if (outputStream != null) {
    : : : outputStream.close();
    : : : }
    : : : }
    : : : [/code]: : :
    : : :
    : : : This has partly resolved my prior issue. I now get part of the
    : : : expected output as the header info is outputted as well as one
    : : : instance of the 4/25/07 run date. However, none of the lines of data
    : : : are being outputted. Any hints as to what I may be doing wrong here.
    : : :
    : : This part of your code seems to be the problem:
    : : [code]: :
    : : : if(l.indexOf("") >= 0) {
    : : : procOn = false;
    : : : }
    : : [/code]: :
    : : This checks if the string l has an empty part in it, which is always
    : : true. [b]If you want to check whether 2 strings are equal, then you
    : : need to use the String.equals() method.[/b]
    :
    : Thanks that code is there to check if the line is blank. I was
    : getting an error message within the procOn part because there is a
    : blank line between the last line of data and the symbols I was using
    : to end it so that the series of ===== is not outputted. What I'm
    : looking to do is turn procOn to false if the entire line is blank.
    : Is there a way to say if line is completely blank do this? Such a
    : certain class, etc I should look into that might resolve for me?
    :
    If you look at my previous reply, you'll see the answer. To check if a string is equal to an empry string use the String.equals() method. For more info see: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#equals(java.lang.Object)
  • adlb1300adlb1300 Posts: 9Member
    I've been able to resolve the blank line issue, however, when the data outputs the line reading "ASC Number Loan ID DelqMessage" first. This causes the output to read like:

    Rundate ASC Loan Delq Message
    4/25/07 ASC LoanID elqMessage
    4/25/07 033 0001010000 N

    I do not want that 1st line to output with the ASC LoanID elqMessage. I created an array that is suppose to process all of the junk lines, ie report lines I do not want in th final output. However, I continue to get the following errors:

    FileParse.java:91: cannot find symbol
    symbol : variable junkRow
    location: class FileParse
    for(int i = 0; i < junkRow.length();i++) {
    ^
    FileParse.java:92: cannot find symbol
    symbol : variable junkRow
    location: class FileParse
    if(x.indexOf(junkRow[i])) {
    ^

    Here is what I have coded which should be correct:

    [code]
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;

    public class FileParse {
    public void main(String[] args) throws IOException {

    // Variable Declaration

    boolean procOn;
    String RunDate = "";
    BufferedReader inputStream = null;
    PrintWriter outputStream = null;
    String [] junkRow;
    junkRow = new String[6];

    // Following initializes elements of array

    junkRow[0] = "Report";
    junkRow[1] = "ASC Number";
    junkRow[2] = "Fake Report";
    junkRow[3] = "Loan Total";
    junkRow[4] = "Total Delq";
    junkRow[5] = "ASC Total";

    // Following block of cod parses FakeReportToParse.txt and outputs
    // needed portions of the report to characteroutput.txt

    try {

    procOn = false;

    inputStream =
    new BufferedReader(new FileReader("FakeReportToParse.txt"));
    outputStream =
    new PrintWriter(new FileWriter("characteroutput.txt"));

    outputStream.println("Rundate ASCNumber Loan DelqMessage");

    String l;
    while ((l = inputStream.readLine()) != null) {

    if (l.indexOf("Run Date:") >= 0) {
    RunDate = l.substring(10);
    }

    // Processing of report data

    if(l.indexOf("ASC Number") >= 0) {
    procOn = true;
    }

    if(l.indexOf("---") >= 0 || l.indexOf("===") >= 0) {
    procOn = false;
    }

    // Output report data to new characteroutput.txt file

    if(procOn) {

    if(!isjunk(l)) {
    String ASC = l.substring(0, 4).trim();
    String LoanID = l.substring(15, 25).trim();
    String DelqM = l.substring(30).trim();

    {
    outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    }
    }
    }
    }
    } catch(Exception z){
    System.out.println("Error: " + z.toString());
    }

    finally {
    if (inputStream != null) {
    inputStream.close();
    }
    if (outputStream != null) {
    outputStream.close();
    }
    }
    }
    // @return true if it's a junk row
    // @return false if it's not

    private boolean isjunk(String x){
    for(int i = 0; i < junkRow.length();i++){
    if(x.indexOf(junkRow[i])){
    return true;
    }
    if(x.trim().length() < 1) {
    return true;
    }
    } return false;
    }
    }
    [/code]

    Any help would be appreciated.

    Thanks
  • zibadianzibadian Posts: 6,349Member
    : I've been able to resolve the blank line issue, however, when the
    : data outputs the line reading "ASC Number Loan ID
    : DelqMessage" first. This causes the output to read like:
    :
    : Rundate ASC Loan Delq Message
    : 4/25/07 ASC LoanID elqMessage
    : 4/25/07 033 0001010000 N
    :
    : I do not want that 1st line to output with the ASC LoanID
    : elqMessage. I created an array that is suppose to process all of the
    : junk lines, ie report lines I do not want in th final output.
    : However, I continue to get the following errors:
    :
    : FileParse.java:91: cannot find symbol
    : symbol : variable junkRow
    : location: class FileParse
    : for(int i = 0; i < junkRow.length();i++) {
    : ^
    : FileParse.java:92: cannot find symbol
    : symbol : variable junkRow
    : location: class FileParse
    : if(x.indexOf(junkRow[i])) {
    : ^
    :
    : Here is what I have coded which should be correct:
    :
    : [code]:
    : import java.io.FileReader;
    : import java.io.FileWriter;
    : import java.io.BufferedReader;
    : import java.io.PrintWriter;
    : import java.io.IOException;
    :
    : public class FileParse {
    : public void main(String[] args) throws IOException {
    :
    : // Variable Declaration
    :
    : boolean procOn;
    : String RunDate = "";
    : BufferedReader inputStream = null;
    : PrintWriter outputStream = null;
    : String [] junkRow;
    : junkRow = new String[6];
    :
    : // Following initializes elements of array
    :
    : junkRow[0] = "Report";
    : junkRow[1] = "ASC Number";
    : junkRow[2] = "Fake Report";
    : junkRow[3] = "Loan Total";
    : junkRow[4] = "Total Delq";
    : junkRow[5] = "ASC Total";
    :
    : // Following block of cod parses FakeReportToParse.txt and outputs
    : // needed portions of the report to characteroutput.txt
    :
    : try {
    :
    : procOn = false;
    :
    : inputStream =
    : new BufferedReader(new FileReader("FakeReportToParse.txt"));
    : outputStream =
    : new PrintWriter(new FileWriter("characteroutput.txt"));
    :
    : outputStream.println("Rundate ASCNumber Loan DelqMessage");
    :
    : String l;
    : while ((l = inputStream.readLine()) != null) {
    :
    : if (l.indexOf("Run Date:") >= 0) {
    : RunDate = l.substring(10);
    : }
    :
    : // Processing of report data
    :
    : if(l.indexOf("ASC Number") >= 0) {
    : procOn = true;
    : }
    :
    : if(l.indexOf("---") >= 0 || l.indexOf("===") >= 0) {
    : procOn = false;
    : }
    :
    : // Output report data to new characteroutput.txt file
    :
    : if(procOn) {
    :
    : if(!isjunk(l)) {
    : String ASC = l.substring(0, 4).trim();
    : String LoanID = l.substring(15, 25).trim();
    : String DelqM = l.substring(30).trim();
    :
    : {
    : outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    : }
    : }
    : }
    : }
    : } catch(Exception z){
    : System.out.println("Error: " + z.toString());
    : }
    :
    : finally {
    : if (inputStream != null) {
    : inputStream.close();
    : }
    : if (outputStream != null) {
    : outputStream.close();
    : }
    : }
    : }
    : // @return true if it's a junk row
    : // @return false if it's not
    :
    : private boolean isjunk(String x){
    : for(int i = 0; i < junkRow.length();i++){
    : if(x.indexOf(junkRow[i])){
    : return true;
    : }
    : if(x.trim().length() < 1) {
    : return true;
    : }
    : } return false;
    : }
    : }
    : [/code]:
    :
    : Any help would be appreciated.
    :
    : Thanks
    :
    You've defined junkRow as a local variable in main(), which means that you can only use it in that method. To use it in other methods, you'll need to pass it as a method parameter, or define it as a class field.
  • adlb1300adlb1300 Posts: 9Member
    : : I've been able to resolve the blank line issue, however, when the
    : : data outputs the line reading "ASC Number Loan ID
    : : DelqMessage" first. This causes the output to read like:
    : :
    : : Rundate ASC Loan Delq Message
    : : 4/25/07 ASC LoanID elqMessage
    : : 4/25/07 033 0001010000 N
    : :
    : : I do not want that 1st line to output with the ASC LoanID
    : : elqMessage. I created an array that is suppose to process all of the
    : : junk lines, ie report lines I do not want in th final output.
    : : However, I continue to get the following errors:
    : :
    : : FileParse.java:91: cannot find symbol
    : : symbol : variable junkRow
    : : location: class FileParse
    : : for(int i = 0; i < junkRow.length();i++) {
    : : ^
    : : FileParse.java:92: cannot find symbol
    : : symbol : variable junkRow
    : : location: class FileParse
    : : if(x.indexOf(junkRow[i])) {
    : : ^
    : :
    : : Here is what I have coded which should be correct:
    : :
    : : [code]: :
    : : import java.io.FileReader;
    : : import java.io.FileWriter;
    : : import java.io.BufferedReader;
    : : import java.io.PrintWriter;
    : : import java.io.IOException;
    : :
    : : public class FileParse {
    : : public void main(String[] args) throws IOException {
    : :
    : : // Variable Declaration
    : :
    : : boolean procOn;
    : : String RunDate = "";
    : : BufferedReader inputStream = null;
    : : PrintWriter outputStream = null;
    : : String [] junkRow;
    : : junkRow = new String[6];
    : :
    : : // Following initializes elements of array
    : :
    : : junkRow[0] = "Report";
    : : junkRow[1] = "ASC Number";
    : : junkRow[2] = "Fake Report";
    : : junkRow[3] = "Loan Total";
    : : junkRow[4] = "Total Delq";
    : : junkRow[5] = "ASC Total";
    : :
    : : // Following block of cod parses FakeReportToParse.txt and outputs
    : : // needed portions of the report to characteroutput.txt
    : :
    : : try {
    : :
    : : procOn = false;
    : :
    : : inputStream =
    : : new BufferedReader(new FileReader("FakeReportToParse.txt"));
    : : outputStream =
    : : new PrintWriter(new FileWriter("characteroutput.txt"));
    : :
    : : outputStream.println("Rundate ASCNumber Loan DelqMessage");
    : :
    : : String l;
    : : while ((l = inputStream.readLine()) != null) {
    : :
    : : if (l.indexOf("Run Date:") >= 0) {
    : : RunDate = l.substring(10);
    : : }
    : :
    : : // Processing of report data
    : :
    : : if(l.indexOf("ASC Number") >= 0) {
    : : procOn = true;
    : : }
    : :
    : : if(l.indexOf("---") >= 0 || l.indexOf("===") >= 0) {
    : : procOn = false;
    : : }
    : :
    : : // Output report data to new characteroutput.txt file
    : :
    : : if(procOn) {
    : :
    : : if(!isjunk(l)) {
    : : String ASC = l.substring(0, 4).trim();
    : : String LoanID = l.substring(15, 25).trim();
    : : String DelqM = l.substring(30).trim();
    : :
    : : {
    : : outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    : : }
    : : }
    : : }
    : : }
    : : } catch(Exception z){
    : : System.out.println("Error: " + z.toString());
    : : }
    : :
    : : finally {
    : : if (inputStream != null) {
    : : inputStream.close();
    : : }
    : : if (outputStream != null) {
    : : outputStream.close();
    : : }
    : : }
    : : }
    : : // @return true if it's a junk row
    : : // @return false if it's not
    : :
    : : private boolean isjunk(String x){
    : : for(int i = 0; i < junkRow.length();i++){
    : : if(x.indexOf(junkRow[i])){
    : : return true;
    : : }
    : : if(x.trim().length() < 1) {
    : : return true;
    : : }
    : : } return false;
    : : }
    : : }
    : : [/code]: :
    : :
    : : Any help would be appreciated.
    : :
    : : Thanks
    : :
    : You've defined junkRow as a local variable in main(), which means
    : that you can only use it in that method. To use it in other methods,
    : you'll need to pass it as a method parameter, or define it as a
    : class field.

    I have been trying to figure out how to pass it as a method parameter or define it as a class field without success. I have tried to do it but cannot even through I have read up on it. Would be able to show me how to do it so I can learn about it or provide me an example?

    I really do appreciate all of your help.
  • zibadianzibadian Posts: 6,349Member
    : I've been able to resolve the blank line issue, however, when the
    : data outputs the line reading "ASC Number Loan ID
    : DelqMessage" first. This causes the output to read like:
    :
    : Rundate ASC Loan Delq Message
    : 4/25/07 ASC LoanID elqMessage
    : 4/25/07 033 0001010000 N
    :
    : I do not want that 1st line to output with the ASC LoanID
    : elqMessage. I created an array that is suppose to process all of the
    : junk lines, ie report lines I do not want in th final output.
    : However, I continue to get the following errors:
    :
    : FileParse.java:91: cannot find symbol
    : symbol : variable junkRow
    : location: class FileParse
    : for(int i = 0; i < junkRow.length();i++) {
    : ^
    : FileParse.java:92: cannot find symbol
    : symbol : variable junkRow
    : location: class FileParse
    : if(x.indexOf(junkRow[i])) {
    : ^
    :
    : Here is what I have coded which should be correct:
    :
    : [code]:
    : import java.io.FileReader;
    : import java.io.FileWriter;
    : import java.io.BufferedReader;
    : import java.io.PrintWriter;
    : import java.io.IOException;
    :
    : public class FileParse {
    : public void main(String[] args) throws IOException {
    :
    : // Variable Declaration
    :
    : boolean procOn;
    : String RunDate = "";
    : BufferedReader inputStream = null;
    : PrintWriter outputStream = null;
    : String [] junkRow;
    : junkRow = new String[6];
    :
    : // Following initializes elements of array
    :
    : junkRow[0] = "Report";
    : junkRow[1] = "ASC Number";
    : junkRow[2] = "Fake Report";
    : junkRow[3] = "Loan Total";
    : junkRow[4] = "Total Delq";
    : junkRow[5] = "ASC Total";
    :
    : // Following block of cod parses FakeReportToParse.txt and outputs
    : // needed portions of the report to characteroutput.txt
    :
    : try {
    :
    : procOn = false;
    :
    : inputStream =
    : new BufferedReader(new FileReader("FakeReportToParse.txt"));
    : outputStream =
    : new PrintWriter(new FileWriter("characteroutput.txt"));
    :
    : outputStream.println("Rundate ASCNumber Loan DelqMessage");
    :
    : String l;
    : while ((l = inputStream.readLine()) != null) {
    :
    : if (l.indexOf("Run Date:") >= 0) {
    : RunDate = l.substring(10);
    : }
    :
    : // Processing of report data
    :
    : if(l.indexOf("ASC Number") >= 0) {
    : procOn = true;
    : }
    :
    : if(l.indexOf("---") >= 0 || l.indexOf("===") >= 0) {
    : procOn = false;
    : }
    :
    : // Output report data to new characteroutput.txt file
    :
    : if(procOn) {
    :
    : if(!isjunk(l)) {
    : String ASC = l.substring(0, 4).trim();
    : String LoanID = l.substring(15, 25).trim();
    : String DelqM = l.substring(30).trim();
    :
    : {
    : outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    : }
    : }
    : }
    : }
    : } catch(Exception z){
    : System.out.println("Error: " + z.toString());
    : }
    :
    : finally {
    : if (inputStream != null) {
    : inputStream.close();
    : }
    : if (outputStream != null) {
    : outputStream.close();
    : }
    : }
    : }
    : // @return true if it's a junk row
    : // @return false if it's not
    :
    : private boolean isjunk(String x){
    : for(int i = 0; i < junkRow.length();i++){
    : if(x.indexOf(junkRow[i])){
    : return true;
    : }
    : if(x.trim().length() < 1) {
    : return true;
    : }
    : } return false;
    : }
    : }
    : [/code]:
    :
    : Any help would be appreciated.
    :
    : Thanks
    :
    Change isJunk() to:
    [code]
    private boolean isjunk(String x, String[] junkRow)
    [/code]
    and then add the junkRow to the parameters. Example:
    [code]
    isjunk(someString, junkRow)
    [/code]

    To make it a class field move the junkRow definition to between the main() and the line containing the class definition:
    [code]
    public class FileParse {

    private static String[] junkRow;

    public void main(String[] args) throws IOException {
    [/code]
    because there is no object instance of FileParse, you need to make junkRow static. The word private means that other classes cannot use it (see variable scopes for more info).
  • adlb1300adlb1300 Posts: 9Member
    : : I've been able to resolve the blank line issue, however, when the
    : : data outputs the line reading "ASC Number Loan ID
    : : DelqMessage" first. This causes the output to read like:
    : :
    : : Rundate ASC Loan Delq Message
    : : 4/25/07 ASC LoanID elqMessage
    : : 4/25/07 033 0001010000 N
    : :
    : : I do not want that 1st line to output with the ASC LoanID
    : : elqMessage. I created an array that is suppose to process all of the
    : : junk lines, ie report lines I do not want in th final output.
    : : However, I continue to get the following errors:
    : :
    : : FileParse.java:91: cannot find symbol
    : : symbol : variable junkRow
    : : location: class FileParse
    : : for(int i = 0; i < junkRow.length();i++) {
    : : ^
    : : FileParse.java:92: cannot find symbol
    : : symbol : variable junkRow
    : : location: class FileParse
    : : if(x.indexOf(junkRow[i])) {
    : : ^
    : :
    : : Here is what I have coded which should be correct:
    : :
    : : [code]: :
    : : import java.io.FileReader;
    : : import java.io.FileWriter;
    : : import java.io.BufferedReader;
    : : import java.io.PrintWriter;
    : : import java.io.IOException;
    : :
    : : public class FileParse {
    : : public void main(String[] args) throws IOException {
    : :
    : : // Variable Declaration
    : :
    : : boolean procOn;
    : : String RunDate = "";
    : : BufferedReader inputStream = null;
    : : PrintWriter outputStream = null;
    : : String [] junkRow;
    : : junkRow = new String[6];
    : :
    : : // Following initializes elements of array
    : :
    : : junkRow[0] = "Report";
    : : junkRow[1] = "ASC Number";
    : : junkRow[2] = "Fake Report";
    : : junkRow[3] = "Loan Total";
    : : junkRow[4] = "Total Delq";
    : : junkRow[5] = "ASC Total";
    : :
    : : // Following block of cod parses FakeReportToParse.txt and outputs
    : : // needed portions of the report to characteroutput.txt
    : :
    : : try {
    : :
    : : procOn = false;
    : :
    : : inputStream =
    : : new BufferedReader(new FileReader("FakeReportToParse.txt"));
    : : outputStream =
    : : new PrintWriter(new FileWriter("characteroutput.txt"));
    : :
    : : outputStream.println("Rundate ASCNumber Loan DelqMessage");
    : :
    : : String l;
    : : while ((l = inputStream.readLine()) != null) {
    : :
    : : if (l.indexOf("Run Date:") >= 0) {
    : : RunDate = l.substring(10);
    : : }
    : :
    : : // Processing of report data
    : :
    : : if(l.indexOf("ASC Number") >= 0) {
    : : procOn = true;
    : : }
    : :
    : : if(l.indexOf("---") >= 0 || l.indexOf("===") >= 0) {
    : : procOn = false;
    : : }
    : :
    : : // Output report data to new characteroutput.txt file
    : :
    : : if(procOn) {
    : :
    : : if(!isjunk(l)) {
    : : String ASC = l.substring(0, 4).trim();
    : : String LoanID = l.substring(15, 25).trim();
    : : String DelqM = l.substring(30).trim();
    : :
    : : {
    : : outputStream.println(RunDate + " " + ASC + " " + LoanID + " " + DelqM);
    : : }
    : : }
    : : }
    : : }
    : : } catch(Exception z){
    : : System.out.println("Error: " + z.toString());
    : : }
    : :
    : : finally {
    : : if (inputStream != null) {
    : : inputStream.close();
    : : }
    : : if (outputStream != null) {
    : : outputStream.close();
    : : }
    : : }
    : : }
    : : // @return true if it's a junk row
    : : // @return false if it's not
    : :
    : : private boolean isjunk(String x){
    : : for(int i = 0; i < junkRow.length();i++){
    : : if(x.indexOf(junkRow[i])){
    : : return true;
    : : }
    : : if(x.trim().length() < 1) {
    : : return true;
    : : }
    : : } return false;
    : : }
    : : }
    : : [/code]: :
    : :
    : : Any help would be appreciated.
    : :
    : : Thanks
    : :
    : Change isJunk() to:
    : [code]:
    : private boolean isjunk(String x, String[] junkRow)
    : [/code]:
    : and then add the junkRow to the parameters. Example:
    : [code]:
    : isjunk(someString, junkRow)
    : [/code]:
    :
    : To make it a class field move the junkRow definition to between the
    : main() and the line containing the class definition:
    : [code]:
    : public class FileParse {
    :
    : private static String[] junkRow;
    :
    : public void main(String[] args) throws IOException {
    : [/code]:
    : because there is no object instance of FileParse, you need to make
    : junkRow static. The word private means that other classes cannot use
    : it (see variable scopes for more info).

    I tried both ways and when I compiled the program I received the following error:

    FileParse.java:92: incompatible types
    found : int
    required: boolean
    if(x.indexOf(junkRow[i])) {
    ^

    I just do not get it. Everytime I fix something anothr issue pops up on me.

    Thanks
«1
Sign In or Register to comment.