Character.isDigit malfunction

Discussion in 'Java' started by soup_or_power@yahoo.com, Feb 8, 2007.

  1. Guest

    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
    , Feb 8, 2007
    #1
    1. Advertising

  2. On 8 Feb 2007 07:22:40 -0800, wrote:
    > 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?


    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

    --
    [ 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
    Gordon Beaton, Feb 8, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 8, 10:40 am, Gordon Beaton <> wrote:
    > On 8 Feb 2007 07:22:40 -0800, wrote:
    >
    > > 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?

    >
    > 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
    >
    > --
    > [ 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
    , Feb 8, 2007
    #3
  4. Oliver Wong Guest

    <> wrote in message
    news:...
    > On Feb 8, 10:40 am, Gordon Beaton <> wrote:
    >> On 8 Feb 2007 07:22:40 -0800, wrote:
    >>
    >> 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!
    >>

    >
    > 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:

    [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
    Oliver Wong, Feb 8, 2007
    #4
  5. Guest

    On Feb 8, 12:00 pm, "Oliver Wong" <> wrote:
    > <> wrote in message
    >
    > news:...> On Feb 8, 10:40 am, Gordon Beaton <> wrote:
    > >> On 8 Feb 2007 07:22:40 -0800, wrote:

    >
    > >> 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!

    >
    > > 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:

    >
    > [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


    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
    , Feb 8, 2007
    #5
  6. Oliver Wong Guest

    <> wrote in message
    news:...
    > On Feb 8, 12:00 pm, "Oliver Wong" <> wrote:
    >> <> wrote in message
    >>
    >> 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);
    >> }
    >>
    >> }
    >>

    >
    > 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
    > }


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

    - Oliver
    Oliver Wong, Feb 8, 2007
    #6
  7. Guest

    On Feb 8, 1:47 pm, "Oliver Wong" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > On Feb 8, 12:00 pm, "Oliver Wong" <> wrote:
    > >> <> wrote in message

    >
    > >> 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);
    > >> }

    >
    > >> }

    >
    > > 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
    > > }

    >
    > Okay, so why don't you use the code that works? ;)
    >
    > - Oliver- Hide quoted text -
    >
    > - 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
    , Feb 8, 2007
    #7
  8. Oliver Wong Guest

    <> wrote in message
    news:...
    > On Feb 8, 1:47 pm, "Oliver Wong" <> wrote:
    >> <> wrote in message
    >> > 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
    >> > }

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

    >
    > Since I had to do lot of debugging before zeroing on the working code,
    > '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
    Oliver Wong, Feb 8, 2007
    #8
  9. Chris Dollin Guest

    wrote:

    > 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/. )

    --
    Chris "electric hedgehog" Dollin
    There' no hortage of vowel on Uenet.
    Chris Dollin, Feb 9, 2007
    #9
  10. <> wrote:
    >> >> 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);
    >> >> }
    >> >> }


    >> > 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
    >> > }


    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?
    Andreas Leitgeb, Feb 9, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach

    isdigit() for characters greater than 127

    Alf P. Steinbach, Oct 9, 2004, in forum: C++
    Replies:
    4
    Views:
    3,221
    James Gregory
    Oct 9, 2004
  2. Carramba

    passing array to isdigit()

    Carramba, Feb 9, 2005, in forum: C Programming
    Replies:
    15
    Views:
    788
    Michael Mair
    Feb 21, 2005
  3. mdh

    isdigit

    mdh, Oct 6, 2006, in forum: C Programming
    Replies:
    10
    Views:
    812
  4. MooMaster
    Replies:
    2
    Views:
    390
    Dan Bishop
    May 1, 2008
  5. Donkey Hottie

    Re: print problem using isDigit

    Donkey Hottie, Sep 29, 2008, in forum: Java
    Replies:
    4
    Views:
    365
    RedGrittyBrick
    Sep 29, 2008
Loading...

Share This Page