checking character problem

J

James Kilroy

Hello I was wondering if you guys could spot a problem with this code

I am taking information from a password field and checking to ensure there
is only letters and digits. It seems to work with errors in JBuilder but
VisualAge is giving me errors. Any ideas

String Password=jpassword.getText(); //get password
int passwordSize=Password.length(); //get length of
password

for (int i=0;i<=passwordSize;i++){

char result=passwordSize.charAt(i);

if(!Character.isJavaLetterOrDigit(result)) {


JOptionPane.showMessageDialog(null,"Password can only
contain letters or digits","Error",
JOptionPane.WARNING_MESSAGE);

}
 
C

Christophe Vanfleteren

James said:
Hello I was wondering if you guys could spot a problem with this code

I am taking information from a password field and checking to ensure there
is only letters and digits. It seems to work with errors in JBuilder but
VisualAge is giving me errors. Any ideas

String Password=jpassword.getText(); //get password
int passwordSize=Password.length(); //get length of
password

for (int i=0;i<=passwordSize;i++){

char result=passwordSize.charAt(i);

if(!Character.isJavaLetterOrDigit(result)) {


JOptionPane.showMessageDialog(null,"Password can only
contain letters or digits","Error",
JOptionPane.WARNING_MESSAGE);

}

You might want to tell us what errors you are exactly getting.
Could it be because the isJavaLetterOrDigit method is deprecated? What
version are you using?
 
M

Manish Hatwalne

James Kilroy said:
Hello I was wondering if you guys could spot a problem with this code

I am taking information from a password field and checking to ensure there
is only letters and digits. It seems to work with errors in JBuilder but
VisualAge is giving me errors. Any ideas

String Password=jpassword.getText(); //get password
int passwordSize=Password.length(); //get length of
password

for (int i=0;i<=passwordSize;i++){

char result=passwordSize.charAt(i);

Did it compile?

Shouldn't it be -

Password.charAt(i);

HTH,
- Manish
 
J

James Kilroy

I am using IBM Visual Age Java 4.0

yes it is deprecated

The message I am getting is as follows

The method charAT invoked for type int with arguments (int) is not defined

The method isJavaLetterOrDigit invoked for type java.lang.Character with
arguments (char) is deprecated
 
C

Christophe Vanfleteren

James said:
I am using IBM Visual Age Java 4.0

yes it is deprecated

The message I am getting is as follows

The method charAT invoked for type int with arguments (int) is not defined

That's because you are trying to run that method on an int, like Manish
pointed out.
The method isJavaLetterOrDigit invoked for type java.lang.Character with
arguments (char) is deprecated

That shouldn't prohibit a successfull compilation, but it is best not to use
deprecated methods.
Read the api docs at
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html#isJavaLetterOrDigit(char)
 
S

Sudsy

James said:
Hello I was wondering if you guys could spot a problem with this code

I am taking information from a password field and checking to ensure there
is only letters and digits. It seems to work with errors in JBuilder but
VisualAge is giving me errors. Any ideas

String Password=jpassword.getText(); //get password
int passwordSize=Password.length(); //get length of
password

for (int i=0;i<=passwordSize;i++){

Ack! You're rolling off the end of the string! It should be
i < passwordSize, not i <= passwordSize. If I have a String
of length 2 then I can only get charAt( 0 ) and charAt( 1 ).
 
G

Gregory A. Swarthout

James Kilroy said:
Hello I was wondering if you guys could spot a problem with this code

I am taking information from a password field and checking to ensure there
is only letters and digits. It seems to work with errors in JBuilder but
VisualAge is giving me errors. Any ideas

String Password=jpassword.getText(); //get password
int passwordSize=Password.length(); //get length of
password

for (int i=0;i<=passwordSize;i++){

char result=passwordSize.charAt(i);

You didn't mention what the error(s) is(are), but I see one here.
Your for
condition shuld be i < passwordSize not i <= passwordSize since
String.charAt(int) is 0-based.

Greg
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top