Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Character.isDigit malfunction

Thread view: 
soup_or_power@yahoo.com - 08 Feb 2007 15:22 GMT
The following code works as expected and breaks when a non-digit is
found in the string test
//string test set somewhere in the code
for(int i=0; i < test.length(); i++) {
                       if (Character.isDigit(test.charAt(i)) )
continue;
                       flg=1;
                       break;
}

The following code does not work.

for(int i=0; i < test.length(); i++) {
                       if (!Character.isDigit(test.charAt(i)) ) {
                                  flg=1;
                                  break;
                       }
}

I tried printing Character.getType(test.charAt(i)) and it is always 9
indicating decimal digit. Can someone please clarify what's being done
wrong?
Thanks
Gordon Beaton - 08 Feb 2007 15:40 GMT
> The following code does not work.
>
[quoted text clipped - 8 lines]
> 9 indicating decimal digit. Can someone please clarify what's being
> done wrong?

There is an error in your *real* code. My results don't agree with
yours, i.e. both posted examples work as advertised.

It would help if you posted an example of the string that gives the
strange result, and also indicated exactly where in the code you put
the call to getType(). In fact why not post a complete, compilable
example that exhibits the strange behaviour. Cut and paste!

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

soup_or_power@yahoo.com - 08 Feb 2007 15:53 GMT
> > The following code does not work.
>
[quoted text clipped - 22 lines]
> [ don't email me support questions or followups ]
> g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

The string is either an encrypted one or a number. I can't post the
encrypted ones. The number strings are like "012345". I put the
getType inside the for loop as follows:

for(int i=0; i < test.length(); i++) {
                         System.out.println("type=" +
Character.getType(test.charAt(i));
                        if (!Character.isDigit(test.charAt(i)) ) {
                                   flg=1;
                                   break;
                        }
}
Here is the complete code I am working with:

import java.io.*;
import java.util.*;

public class csv4{
       public static Hashtable returns_apps = new Hashtable();
       public static Hashtable returns_ssn= new Hashtable();
       public static Hashtable returns_btn= new Hashtable();
       public static Hashtable tzmap=new Hashtable();
  public static void main( String [] args) {
       String aline="";
   try {
       FileReader fr = new FileReader("return_ssn_020807.csv");
       BufferedReader br = new BufferedReader(fr);
       while ((aline = br.readLine()) != null) {
               returns_apps.put(aline.trim(), "");
       }
       br.close();
       fr = new FileReader(args[0]);
       br = new BufferedReader(fr);
       while ((aline = br.readLine()) != null) {
               aline=aline.replaceAll(",", " , ");
               //aline=aline.replaceAll("\"", "");
               String [] aline_arr = aline.split(",");
               if (aline_arr.length < 6) continue;
               String ssn =aline_arr[5].trim();
               int flg=0;
               for(int i=0; i < ssn.length(); i++) {
                       System.err.println("char" +
Character.getType(ssn.charAt(i)));

                       System.err.println("i=" + i);
                       if (Character.isDigit(ssn.charAt(i)) )
continue;
                       flg=1;
                       break;
               }
               if (flg == 1) {
                       continue;
               }
               if (returns_apps.containsKey(ssn))
               {
                       continue;
               }
               System.out.println(aline);
       }
       br.close();
    } catch (Exception e) {
               System.out.println("exception " + e.getMessage());
               e.printStackTrace();
    }
   } //main
} //class

Thanks
Oliver Wong - 08 Feb 2007 17:00 GMT
>> There is an error in your *real* code. My results don't agree with
>> yours, i.e. both posted examples work as advertised.
[quoted text clipped - 7 lines]
> encrypted ones. The number strings are like "012345". I put the
> getType inside the for loop as follows:
[code snipped]

Here's a simplified version of your code (with all the file IO junk
removed), and a few extra debugging statements added. If you run it on your
strings, do you still get the same problem?

public class csv4 {
public static void main(String[] args) {
 String ssn = "Manually type your string here";
 boolean containsNonDigit = false;
 for (int i = 0; i < ssn.length(); i++) {
  System.err.println("i=" + i);
  System.err.println("char=" + ssn.charAt(i));
  System.err.println("chartype=" + Character.getType(ssn.charAt(i)));
  System.err.println("isDigit=" + Character.isDigit(ssn.charAt(i)));
  if (Character.isDigit(ssn.charAt(i))) {
   continue;
  }
  containsNonDigit = true;
  break;
 }
 System.out.println("containsNonDigit is " + containsNonDigit);
}
}

   - Oliver
soup_or_power@yahoo.com - 08 Feb 2007 18:43 GMT
> <soup_or_po...@yahoo.com> wrote in message
>
[quoted text clipped - 39 lines]
>
>     - Oliver

As I indicated earlier the following code worked:

if (Character.isDigit(ssn.charAt(i))) {
    continue;
   }

What doesn't work is
if (!Character.isDigit(ssn.charAt(i))) {
   //set flag
   }

Thanks
Oliver Wong - 08 Feb 2007 18:47 GMT
>> <soup_or_po...@yahoo.com> wrote in message
>>
[quoted text clipped - 33 lines]
>    //set flag
>    }

   Okay, so why don't you use the code that works? ;)

   - Oliver
soup_or_power@yahoo.com - 08 Feb 2007 18:58 GMT
> <soup_or_po...@yahoo.com> wrote in message
>
[quoted text clipped - 43 lines]
>
> - Show quoted text -

Since I had to do lot of debugging before zeroing on the working code,
'thought others can comment/benefit from it.
Thanks
Oliver Wong - 08 Feb 2007 19:07 GMT
>> <soup_or_po...@yahoo.com> wrote in message
>> > As I indicated earlier the following code worked:
[quoted text clipped - 13 lines]
> 'thought others can comment/benefit from it.
> Thanks

   Ah, okay. I thought you were asking for help about a problem, not
telling others about the problem you fixed. Glad it's settled, then.

   - Oliver
Chris Dollin - 09 Feb 2007 08:26 GMT
> Since I had to do lot of debugging before zeroing on the working code,
> 'thought others can comment/benefit from it.

My only-skimmed impression is: no method with that many `continue`s in
it can be right, and even if it's right, it's not maintainable.

(As far as I can see, the code I've written for Jena - a reasonable amount -
has no `continue`s in it /at all/. )

Signature

Chris "electric hedgehog" Dollin
There' no hortage of vowel on Uenet.

Andreas Leitgeb - 09 Feb 2007 14:36 GMT
>> >> public class csv4 {
>> >>  public static void main(String[] args) {
[quoted text clipped - 14 lines]
>> >>  }
>> >> }

>> > As I indicated earlier the following code worked:
>> > if (Character.isDigit(ssn.charAt(i))) {
[quoted text clipped - 4 lines]
>> >    //set flag
>> > }

Now, that's strange.  I can't reproduce this difference.
Could you mail me (per email, not post) both the two
csv4.java versions and the two compiled class-files
and also tell me which java-compiler you were using?

Which way did the prog fail?
Did it fail to see some non-digits, or
did it report non-digits when none were there?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.