Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop through a string

 
Anna Jean
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Dmitry!
I really appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic