title: How to count the number of dashes (-) in a phone number String for input validation?
body: <p>What I have is a program that reads in a user-entered phone number and returns the country code (if it exists), area code (if it exists), and local 7-digit phone number.<br />
  The number must be entered as countrycode-area-local. So, the maximum number of dashed you can have in the phone number is two.</p>
  <p>For example:<br />
  1-800-5555678 has two dashes (it has a country code and area code)<br />
  800-5555678 has one dash (it only has an area code)<br />
  5555678 has no dashes (only local number)</p>
  <p>So, it's OK to have zero dashes, one dash, or two dashes, but no more than two.  <p><br />
  What I am trying to figure out is how you would count the number of dashes (&quot;-&quot;) in the string to make sure there aren't more than two instances of them. If there are, it would print an error.</p>
  <p>So far, I have:</p>
  <pre><code>if(phoneNumber ///contains more more than two dashed
  {
      System.out.println(&quot;Error, your input has more than two dashes. Please input using the specified format.&quot;);
  {
  else
  {
      //normal operations
  }
  </code></pre>
  <p>Everything works so far except for this one part. I'm not sure what method to use to do this. I tried looking at indexOf, but I am stumped.</p>