Hy,

I have to write a program witch recognizes the extension of the file it gets as argument after that it has to validate the file line by line.
A file should look like this:
ex:

Name, Age, Email, OptionalField1, OptionalField2
Anna, 22, anna@smting.com, completed
Mark, 24, mark@smting.com, completed completed
Alex, 22, alex@smting.com, completed
Tim, 32, tim@smting.com,

It has to validate the mandatory fields( name, age, email) and also the optional fields if they are completed.
It has to validate the age as an Integer

Any idea how can i do this?

Thank you

Recommended Answers

All 4 Replies

check the path of your file (if it has to be a .txt file => if !path.endswith(".txt")) false

your values in your file : read them all into variables and check the values you get.
but you'll need to be a bit more precise into how you want to validate your values, and you should provide us with the code you've written so far, we 're not going to write it for you.

i will write the code myself i just need some pointers
i have to decide what to use, StringTokenizer, Scanner, etc.
i have to make the code very flexible, and i have to make objects with the input i get

well ... what exactly is your problem then?
the easiest way is to write your file the way you know it won't give any problems and write the code you need, then, if you manage to complete it that far without any problems, add validation element by element and test it while going.
that should make it easier to get to what you want compared to trying everything at once.

I found a solution for a while now and I decided to post it, in case anyone needs it

in this solution it doesn't matter the order of the fields and it also accept optional fields

I have an enum with the fields I can get from the file:

public enum Fields {
    FIRST_NAME,
    AGE,
    EMAIL
}

I have an ordinary class with a constructor and setters for the fields, in this class I also have an array to keep any columns

public Map<String, String> otherCol = new HashMap<String, String>();

in the class that handles the file I have an map to hold the columns:

Map<String, Fields> columnsOrderedMap = new HashMap<String, Fields>();

in the class's constructor I pupulate the columns map one by one with all the possible fields it can get:

Map<String, Fields> columns = new HashMap<String, Fields>();
columns.put("First Name", Fields.FIRST_NAME);
columns.put("Age", Fields.AGE);
columns.put("Email", Fields.EMAIL);

then I read the file line by line

int nrOfTokens = tokens.length;
if (lineNr == 1) {
                fields = tokens;
                nrOfFields = nrOfTokens;
                for (int i = nrOfFields - 1; i >= 0; i--) {
                    if(columns.containsKey(tokens[i]))
                        columnsOrderedMap.put(tokens[i], columns.get(tokens[i]));
                    else
                        columnsOrderedMap.put(tokens[i], ContactsFields.OTHER);
                }

and for each value in map:

for(Map.Entry<String, Fields> entry : columnsOrderedMap.entrySet()) {
                    int index = searchPosition(fields, entry.getKey());
                    switch(entry.getValue()) {
                        case FIRST_NAME:
                            contactsTemplate.setFirstName(tokens[index]);
                            break;
//...

function to get the right possition of each field in file

public int searchPosition(String[] argsList, String toSearch) {
        int position = 0;
        int count = -1;
        for(String s: argsList) {
            count++;
            if(s.equals(toSearch)) {
                position = count;
                break;
            }
        }
        return position;
    }

I hope it is helpful for someone, and i apologize because i don't know to explain very well:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.