Determining if a substring is empty - seems easy but I'm stuck

lauren4110
lauren4110 used Ask the Experts™
on
Hi. I am rather new to java and am stuck on something that seems like it should be easy.  I am reading in several lines/records from a text file into a buffer.  Each line is about 80 characters, however, the first 20 characters make up field1. Thus, I used a substring (0,19) on the line to get field1 formy object test:
test.field1 = line.substring(0,19)

This seems to be working ok.  The problem I have is that within the 20 characters of field1, there might really be less than 20 characters/numbers  (actually this is the case more often than not) and the field is made up of a mix of letters and numbers.  For example, "98A" "203456TTYCLOV" "1234567891" "bb22bb" could all be field1.  I am now trying to find all the field1 where it is exactly 10 numbers ...no letters, no more  than 10 numbers, etc, such as "1231231234"
First step, I need to determine that the length is actually just 10 (then I will figure out whether they are letters or numbers, etc). To find the actual length.....
If tried the length method, I always get 20 regardless, even if the last 10 "spaces" are empty.  
So, then I tried to find if the eleventh character (really the 10th position) is null, and that just gave me error message after error message regardless of what I tried. Next, I tried to set up a method checkNum which would return true or false.  Within this method, I set up another substring on field1which pulls in everything from the 11th character on...
String eleventhOn =  this.field1.substring(10,19)

I then went to compare whether this string eleventhOn was empty/null and if so, return True.  However, it never seems to find a positive comparison. It always returns false, though I know there are instances where evelenthOn is empty...I even have it printing the string out on the screen and sure enough, there are times when it prints blanks, so I know I am getting the right substring. I have tried various operators, including ==null, equals(""), etc, but I cannot seem to get it to work (no error messages, it just doesn't seem to recognize when the last 10 slots are blank)

Any help would be great! Thanks

And, if you could also tell me how on earth to compare a char, I would really appreciate it (and can award more points..as I am sure I will need that comparison too). Thanks!

FYI- right now, I am only concerned w/fields that are greater than 10, and not worried about the ones less than.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Wouldn't it be simpler to simply to use class RandomAccessFile and use the functions String readLine() and void writeBytes(String s)? The function readLine simply reads in a string until it encounters a '\n' character and in like manner writeBytes writes a string until a '\n' is encountered. The advantage of these functions is that your field1 entries could be saved with varying lengths. You could then read them in again with readLine and simply use the String's length function to discover the length.

Well, actually write byteBytes writes all of the characters that the string contains but if you need to be sure that you append a '\n' onto the end of it or it will not read back in properly.
Jim CakalicSenior Engineer

Commented:
Why not trim the substring?
    test.field1 = line.substring(0,19).trim();

Now test.field1 should be only the non-blank value. Only downside here is that trim removes whitespace from both ends of the String. But if that isn't a problem, you now have a String that should be less confusing to work with.

Best regards,
Jim Cakalic
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Top Expert 2016

Commented:
What version of Java are you using?
Senior Engineer
Commented:
Didn't see the second question in your post until now. For your purposes, the easiest way is probably to loop through the characters of the String and use the Character.isDigit method:

    test.field1 = line.substring(0,19).trim();
    if (test.field1.length() == 10) {
        boolean allDigits = true;
        for (int i = test.field1.length() - 1; i >= 0 && allDigits; ++i) {
            allDigits = Character.isDigit(test.field1.charAt(i));
        }
    }

CEHJ probably asked what version of the JDK you were using to know if you might be able to apply the new 1.4 regular expressions. That would be another way to go.

Jim

Commented:
Once the string is obtained as mentioned by others, you can check if its a number in the following way:

try {
  int i = Integer.parseInt(testField1);
  //string contains all numbers
} catch(Exception e) {
    //string isn't made of only numbers
}

Commented:
Rather than checking for 'Exception', you can be more specific by checking for 'NumberFormatException'. Though, this will not make any difference.
Jim CakalicSenior Engineer

Commented:
Hmm. Integer.parseInt would work. The only problem is the expense of the exception throwing when the value isn't completely numeric. After fixing the defect in my previous post (the loop index should be decremented, not incremented, in the for) I tested the two methods on JDK 1.4 using a 10 character string. When the value was numeric the two methods time out pretty much equal (the Character.isDigit method is marginally faster). However, when any character of the string is not numeric, isDigit method shows no difference in performance. In fact, it gets faster when the non-numeric character is not at the 'end' of the string.

Here again is the corrected version of my previous post:

    test.field1 = line.substring(0,19).trim();
    if (test.field1.length() == 10) {
        boolean allDigits = true;
        for (int i = test.field1.length() - 1; i >= 0 && allDigits; --i) {
            allDigits = Character.isDigit(test.field1.charAt(i));
       }
   }

Jim

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial