-2

Brand new to Groovy and Java.

I have a large, tab delimited text file.

I need to be able 'test' each line of the text file to make sure certain columns have correct data.

For example,

column 2 should only have the term 'New Customer'
column 14 should only have the term 'Dog' or 'Cat'

If any compare fails, then print the incorrect term.

package TestImport

import java.io.BufferedReader;
import java.io.FileReader;

public class PreValidateData {

    public static void main(String args[]) throws Exception {

        String dataFileName = "C:/Users/BigDaddy/Desktop/test.csv"
        BufferedReader bReader = new BufferedReader(new FileReader(dataFileName));
        String line;

        while (line = bReader.readLine()) {

            //Not sure what to put here.  This doesn't work
            String datavalue[] = line.split("\t");
            String value2 = datavalue[1];
            String value14 = datavalue[13];

            if(value2 != "New Customer"){
                Println("FAILURE: line:" + value2.linenumber + "in column 2 is not New Customer.  but = " + datavalue[2])
            }

            if(value14 != "Ndog" or "cat){
                Println("FAILURE: line:" + value14.linenumber + "in column 14 is not cat or dog.  but = " + term)
            }

        }
        bReader.close();
    }
}

I'm not sure where to even begin. Any suggestions?

cfrick
  • 35,203
  • 6
  • 56
  • 68
GreetRufus
  • 421
  • 2
  • 9
  • 19

2 Answers2

3

Please, use Groovy to do this. Writing this in Java is a waste of keystrokes.

Example:

def file = new File("C:/Users/BigDaddy/Desktop/test.csv")
def lineCount = 0
file.eachLine { line ->
    def parts = line.split '\t'
    assert parts.size() > 13
    if ( parts[ 1 ] != 'New Customer' )
       System.err.println "Failure! Line $lineCount in column 2..."
    if ( ! ( parts[ 13 ] in [ 'dog', 'cat' ] ) )
       System.err.println "Failure! Line $lineCount in column 14..."
    lineCount++
}

Runs almost as fast as Java but is much nicer to write/read.

Renato
  • 12,940
  • 3
  • 54
  • 85
  • 1
    if you want to ignore a line and use a certain encoding, just pass those parameters to the `eachLine` method: `file.eachLine( 'utf-8', 1 ) { ... }` – Renato Nov 03 '14 at 21:34
1

Your syntax is close, you could use a try-with-resources and I would use formatted io (printf). Something like,

try (BufferedReader bReader = new BufferedReader(new FileReader(
        dataFileName))) {
    String line;
    int lineNumber = 0;
    while ((line = bReader.readLine()) != null) {
        lineNumber++;
        String datavalue[] = line.split("\t");
        String value2 = datavalue[1];
        String value14 = datavalue[13];

        if (!value2.equals("New Customer")) {
            System.err.printf("FAILURE: line: %d in column 2 is "
                    + "%s not 'New Customer'.%n", lineNumber, value2);
        }
        if (!(value14.equals("dog") || value14.equals("cat"))) {
            System.err.printf("FAILURE: line: %d in column 14 is "
                    + "%s not 'dog' or 'cat'.%n", lineNumber, value14);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249