aspose file tools*
The moose likes Beginning Java and the fly likes loop through a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loop through a string" Watch "loop through a string" New topic
Author

loop through a string

Anna Jean
Greenhorn

Joined: Dec 23, 2003
Posts: 8
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

Joined: Dec 18, 2003
Posts: 328
need your help to understand the problem. What kind of "numbers" does a string contain? What else does the string contain?
Anna Jean
Greenhorn

Joined: Dec 23, 2003
Posts: 8
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

Joined: Dec 18, 2003
Posts: 328
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

Joined: Dec 23, 2003
Posts: 8
Thanks Dmitry!
I really appreciate your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: loop through a string