Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Character.isDigit malfunction (http://www.velocityreviews.com/forums/t390714-character-isdigit-malfunction.html)

soup_or_power@yahoo.com 02-08-2007 03:22 PM

Character.isDigit malfunction
 

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 02-08-2007 03:40 PM

Re: Character.isDigit malfunction
 
On 8 Feb 2007 07:22:40 -0800, soup_or_power@yahoo.com 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

soup_or_power@yahoo.com 02-08-2007 03:53 PM

Re: Character.isDigit malfunction
 
On Feb 8, 10:40 am, Gordon Beaton <n...@for.email> wrote:
> On 8 Feb 2007 07:22:40 -0800, soup_or_po...@yahoo.com 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


Oliver Wong 02-08-2007 05:00 PM

Re: Character.isDigit malfunction
 

<soup_or_power@yahoo.com> wrote in message
news:1170950008.920346.298190@q2g2000cwa.googlegro ups.com...
> On Feb 8, 10:40 am, Gordon Beaton <n...@for.email> wrote:
>> On 8 Feb 2007 07:22:40 -0800, soup_or_po...@yahoo.com 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



soup_or_power@yahoo.com 02-08-2007 06:43 PM

Re: Character.isDigit malfunction
 
On Feb 8, 12:00 pm, "Oliver Wong" <o...@castortech.com> wrote:
> <soup_or_po...@yahoo.com> wrote in message
>
> news:1170950008.920346.298190@q2g2000cwa.googlegro ups.com...> On Feb 8, 10:40 am, Gordon Beaton <n...@for.email> wrote:
> >> On 8 Feb 2007 07:22:40 -0800, soup_or_po...@yahoo.com 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


Oliver Wong 02-08-2007 06:47 PM

Re: Character.isDigit malfunction
 

<soup_or_power@yahoo.com> wrote in message
news:1170960193.534031.125100@s48g2000cws.googlegr oups.com...
> On Feb 8, 12:00 pm, "Oliver Wong" <o...@castortech.com> wrote:
>> <soup_or_po...@yahoo.com> 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



soup_or_power@yahoo.com 02-08-2007 06:58 PM

Re: Character.isDigit malfunction
 
On Feb 8, 1:47 pm, "Oliver Wong" <o...@castortech.com> wrote:
> <soup_or_po...@yahoo.com> wrote in message
>
> news:1170960193.534031.125100@s48g2000cws.googlegr oups.com...
>
>
>
>
>
> > On Feb 8, 12:00 pm, "Oliver Wong" <o...@castortech.com> wrote:
> >> <soup_or_po...@yahoo.com> 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


Oliver Wong 02-08-2007 07:07 PM

Re: Character.isDigit malfunction
 

<soup_or_power@yahoo.com> wrote in message
news:1170961127.966777.141980@a34g2000cwb.googlegr oups.com...
> On Feb 8, 1:47 pm, "Oliver Wong" <o...@castortech.com> wrote:
>> <soup_or_po...@yahoo.com> 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



Chris Dollin 02-09-2007 08:26 AM

Re: Character.isDigit malfunction
 
soup_or_power@yahoo.com 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.


Andreas Leitgeb 02-09-2007 02:36 PM

Re: Character.isDigit malfunction
 
soup_or_power@yahoo.com <soup_or_power@yahoo.com> 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?



All times are GMT. The time now is 02:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.