Character.isDigit malfunction

S

soup_or_power

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
 
G

Gordon Beaton

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
 
S

soup_or_power

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

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
 
O

Oliver Wong

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
 
S

soup_or_power

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
 
O

Oliver Wong

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
 
S

soup_or_power

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
 
O

Oliver Wong

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
 
C

Chris Dollin

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

Andreas Leitgeb

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top