• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

loop through a string

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am new to Java and wanted to get your thoughts on the best way to validate that all numbers in a string are not the same.
Any assistance would be greatly appreciated.
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
need your help to understand the problem. What kind of "numbers" does a string contain? What else does the string contain?
 
Anna Jean
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Thank you for responding to my post. The string will contain numeric numbers. For example (88888). I want to validation that will determine if all numbers in the string are the same. Does this make sense?
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the string is not null, and contains nothing but numeric numbers
public boolean test(String s) {
if(s.length()<2) return true;
char t = s.charAt(0);
for(int i = 1; i<s.length(); ++i) {
if(t!=s.charAt(i)) return false;
}
return true;
}

I would not say it's "the best", but it will do...
 
Anna Jean
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dmitry!
I really appreciate your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic