Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Last Questiion for today I hope char.length

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static boolean is_special(char value)

{
char[] special_chars = {'#', '$', '%', '&', '*', '.', ','};

for (int i = 0; i!=special_chars.length(); i++)
{
if (value==special_chars[i])
return true;
}
return false;
}

I dont think it likes this line
special_chars.length()
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony,
You are correct. The method length is not defined for arrays. It is defined for String however:
String s = "Yesterday";
int i = s.length(); // Equals 9.
You want the field length that is defined for arrays:
String[] as = {"Yesterday", "Today", "Tommorrow"};
int i = as.length; // Equals 3.
Regards,
Manfred.
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
length is a property of Arrays, so you could use spec_char.length
 
Colin Kenworthy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, length is not a method but a property of arrays so you just need to drop the '()'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic