Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Character.isDigit malfunction

Reply
Thread Tools

Character.isDigit malfunction

 
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      02-08-2007

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

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      02-08-2007
On 8 Feb 2007 07:22:40 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      02-08-2007
On Feb 8, 10:40 am, Gordon Beaton <(E-Mail Removed)> wrote:
> On 8 Feb 2007 07:22:40 -0800, (E-Mail Removed) 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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      02-08-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Feb 8, 10:40 am, Gordon Beaton <(E-Mail Removed)> wrote:
>> On 8 Feb 2007 07:22:40 -0800, (E-Mail Removed) 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


 
Reply With Quote
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      02-08-2007
On Feb 8, 12:00 pm, "Oliver Wong" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...> On Feb 8, 10:40 am, Gordon Beaton <(E-Mail Removed)> wrote:
> >> On 8 Feb 2007 07:22:40 -0800, (E-Mail Removed) 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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      02-08-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Feb 8, 12:00 pm, "Oliver Wong" <(E-Mail Removed)> wrote:
>> <(E-Mail Removed)> 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


 
Reply With Quote
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      02-08-2007
On Feb 8, 1:47 pm, "Oliver Wong" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
>
>
>
>
> > On Feb 8, 12:00 pm, "Oliver Wong" <(E-Mail Removed)> wrote:
> >> <(E-Mail Removed)> 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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      02-08-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Feb 8, 1:47 pm, "Oliver Wong" <(E-Mail Removed)> wrote:
>> <(E-Mail Removed)> 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


 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      02-09-2007
(E-Mail Removed) 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.

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      02-09-2007
(E-Mail Removed) <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What Is Your Major Malfunction!!!! NYC XYZ Firefox 13 11-01-2005 09:28 PM
Frames causes back-button to malfunction..... any remedies? Matt MC HTML 6 04-03-2005 08:39 AM
outlook express malfunction Ron Computer Support 1 01-10-2004 04:50 PM
Major XP malfunction, please help! John Wilkinson Computer Support 31 11-10-2003 05:08 AM
Win Restart malfunction jl Computer Support 0 07-28-2003 11:38 AM



Advertisments