Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Paul Andrew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the static isLetter() method of character class along with a boolean varable:

[ March 26, 2006: Message edited by: Garrett Rowe ]
 
Paul Andrew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not using 1.5 which is needed for such loops unfortunately, (im on uni computers)

Thanks anyhow.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Andrew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic