• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could just translate it into a non for-each loop.

[ March 26, 2006: Message edited by: Garrett Rowe ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic