Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes checking a string for illegal characters..help required.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checking a string for illegal characters..help required...." Watch "checking a string for illegal characters..help required...." New topic
Author

checking a string for illegal characters..help required....

Paul Andrew
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Hello.

I am trying to make my program read a token from a string, and print out an error if the token contains invalid characters, ie smit4h.

I have this....


which seems to work, although i dont really understand how I got here.

Is there a better way to complete this??

Many Thanks


EDIT: Another item in the program is puzzling me also.

If a date string in the program is too long or short, format(dd-mm-yy)
another error is printed.

writing this

works by itself,
but if i put it in the main program, as a method, it prints out all dates as errors, regardless of if they are wrong or not. Any ideas?
[ March 26, 2006: Message edited by: Paul Andrew ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could use the static isLetter() method of character class along with a boolean varable:

[ March 26, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Paul Andrew
Greenhorn

Joined: Mar 24, 2006
Posts: 4
I'm not using 1.5 which is needed for such loops unfortunately, (im on uni computers)

Thanks anyhow.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You could just translate it into a non for-each loop.

[ March 26, 2006: Message edited by: Garrett Rowe ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Note also the important point that Garrett has used "myCharArray.length" instead of '53'. Using numbers like 53 is bad for all kinds of reasons; one reason is that figuring them out in the first place is extra work. A more important reason is that if the code ever must change, finding every instance of '53' may be hard to do. Better to use a variable containing the value, or compute the value as needed, the way it's been done here.


[Jess in Action][AskingGoodQuestions]
Paul Andrew
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Great, thanks for the help guys, the code looks better already.
I have just one little thing to add to this, say i also had to allow commas to be classed as valid, which commands would to this.

eg.

if (!Character.isLetter(array1[i])) || ,

point noted about the variables, it was annoying to type in the letters
[ March 26, 2006: Message edited by: Paul Andrew ]
Mahadevan Gorti SS
Greenhorn

Joined: Jan 31, 2006
Posts: 18
You can try Regular expression (if your JDK > 1.4) like:



[ March 26, 2006: Message edited by: Mahadevan Gorti SS ]
[ March 27, 2006: Message edited by: Mahadevan Gorti SS ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Paul Andrew:
Great, thanks for the help guys, the code looks better already.
I have just one little thing to add to this, say i also had to allow commas to be classed as valid, which commands would to this.

eg.

if (!Character.isLetter(array1[i])) || ,

point noted about the variables, it was annoying to type in the letters

[ March 26, 2006: Message edited by: Paul Andrew ]


Or you could try:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checking a string for illegal characters..help required....