1

I am reading a text file abc .txt which is tab delimited as shown below

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   
rhf  dhh  fhf
kji  idj  ddt

and I am able to read it successfully (for all the columns in the table I have developed a separate pojo along with getters and setters), the advantage is that I can get the value of complete row through its getters.

Now I have to make sure that no column in the table should be null and if any column value is null, then I should throw a new exception, for example ..

gfh  hgh  thf
---  ---  ---
fgh  sji  irj   //row 1
rhf  dhh  fhf
kji  idj  ddt
fgq       fio   //As seen in this row that second column value is null 

Now the approach that I am following is that getting the value row wise in a string as shown below

String n = f.getgfh()+f.gethgh()+f.getthf();  //it will contain the contents of the row 1

make a separate method and will pass this string

private boolean validaterow(String f)
{
}

in this method here I am taking the complete row in a string and inside this method I want to break this string in tokens and then further evaluate those tokens for empty or null , if there are then I will return false

10
  • How are you reading the file ? Commented May 2, 2013 at 6:34
  • @user2200150 you already throw an exception when at least one field is null. What your exact problem? Can you show some code to understand your real problem? Commented May 2, 2013 at 6:38
  • @LuiggiMendoza what I am thinking it is the right approach..! Commented May 2, 2013 at 6:41
  • @user2200150 from the current description of your problem, there's no right approach, since you can opt for different designs. If you just need to throw an exception when at least one of the fields is null, then you already have it. You can also check if the field is null and throw an exception with a more detailed message for every field. Another option can be done using reflection. Again: there's no exact answer for this, it will depend on your needs. Commented May 2, 2013 at 6:44
  • 1
    You should really use a framework like OpenCSV to parse such files. Commented May 2, 2013 at 9:25

7 Answers 7

1

Try this,

private boolean validateRow(String f)
{
if(f.getgfh() != null && !f.getgfh().trim().isEmpty() &&
 f.gethgh() != null && !f.gethgh().trim().isEmpty() &&
            f.getthf() != null && !f.getthf().trim().isEmpty())
    {
        return true;
    }
    else
    {
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can set a null check like in getters

public String gethgh() {

  return (String) ((null == hgh) ? new UserDefinedException() : hgh); 

}

here UserDefinedException must be declared as a class

I consider this should work

Comments

0

Why do you pass it to a method?

Use

String row1,row2,row3;
row1=f.getgfh();
row2=f.gethgh();
row3=f.getthf();

You can check for your condition at the time of concatenation.

if(row1==null)
 'throw your message
else
 row1=""
if(row2==null)
 'throw your message
else
 row2=""
if(row3==null)
 'throw your message
else
 row3=""

At last,

String n = row1+row2+row3;

Comments

0

Look at apache string utils library. It might have what you need

Comments

0

Split each line on tab like string.split("\t") and check each token using isEmpty() method of string class.

Comments

0

use StringIsNullOrEmtpy to check if row is valid for every entry of the row

private boolean ValidateRow()
{
   return !(StringIsNullOrEmpty((f.getgfh())) || 
            StringIsNullOrEmpty((f.gethgh())) ||
            StringIsNullOrEmpty((f.getthf()))
            );
}

private bool StringIsNullOrEmtpy(string input)
{
   return input.isEmpty() || input == null;
}

1 Comment

@WhiletrueSleep I can not founf in java 5 string class
0

Let's assume that you have a method getNextValueAndPosPair() which returns the next value of your tsv (tab separate values) file and the position of the last space found after the value read (i.g. a Tab or a Newline).

Then you can validate your input row by chechking that it contains exactly three distinct values separated by a Tab.

Below the implementation:

// A simple general pair class.
class Pair<A, B> {

    Pair(A first, A second) {
        this.first = first;
        this.second = second;        
    }

    public A first() { return first; }


    public A second() { return second; }


    private final A first;

    private final B second;
}

// Returns true if this rows contains 4 distinct values, false otherwise.
private boolean validaterow(String f) {
    int pos = 0;
    for (int i = 0; i < 3; i++) {
        Pair<String, Integer> valueAndPos = getNextValueAndPosPair(f, pos);
        String value = valueAndPos.first();
        int pos = valueAndPos.second();
        if (value.isEmpty()) {
            System.err.println("The " + i + "-th value is null.");
            return false;
        }
        pos += 1;
    }
    return true;
}

// Returns a Pair holding the next tsv value in the row and the position of the delimiter space
private Pair<String, Integer> getNextValueAndPosPair(String f, int start) {
    int length = 0;
    for (int i = start; i < f.length && !Character.isSpaceChar(f.charAt(i)); i++) length++;

    int end = start + length;
    String value = f.substring(start, end);
    Pair<String, Integer> valueAndPos = new Pair<>(value, end);
    return valueAndPos;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.