Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a loop to search program
    primarykey
    data
    text
    <p>I have the following program in which a user enter his social security number and is assigned a location that correspond to it.</p> <p>One thing I do not get is how can I add a loop to search through through my array until it find the correct area number and the area it correspond to for example 3 correspond to new hampshire. I tried a loop in my program but I am not sure how to make it work. </p> <pre><code>import jpb.*; public class SSNInfo { public static void main(String[] args) { SimpleIO.prompt("Enter a Social Security number: "); String ssn = SimpleIO.readLine().trim(); while (true) { if (ssn.length() != 11) { System.out.println("Error: Number must have 11 " + "characters"); } else if (ssn.charAt(3) != '-' || ssn.charAt(6) != '-') { System.out.println( "Error: Number must have the form ddd-dd-dddd"); } else break; SimpleIO.prompt("\nPlease re-enter number: "); ssn = SimpleIO.readLine().trim(); } // Get the area number (the first 3 digits of the SSN) int area = Integer.parseInt(ssn.substring(0, 3)); int[] areaNumbers = { 3, 7, 9, 34, 39, 49, 134, 158, 211, 220, 222, 231, 236, 246, 251, 260, 267, 302, 317, 361, 386, 399, 407, 415, 424, 428, 432, 439, 448, 467, 477, 485, 500, 502, 504, 508, 515, 517, 519, 520, 524, 525, 527, 529, 530, 539, 544, 573, 574, 576, 579, 580, 584, 585, 586, 588, 595, 599, 601, 626, 645, 647, 649, 653, 658, 665, 675, 679, 680}; String[] locations = {"New Hampshire", "Maine", "Vermont", "Massachusetts", "Rhode Island", "Connecticut", "New York", "New Jersey", "Pennsylvania", "Maryland", "Delaware", "Virginia", "West Virginia", "North Carolina", "South Carolina", "Georgia", "Florida", "Ohio", "Indiana", "Illinois", "Michigan", "Wisconsin", "Kentucky", "Tennessee", "Alabama", "Mississippi", "Arkansas", "Louisiana", "Oklahoma", "Texas", "Minnesota", "Iowa", "Missouri", "North Dakota", "South Dakota", "Nebraska", "Kansas", "Montana", "Idaho", "Wyoming", "Colorado", "New Mexico", "Arizona", "Utah", "Nevada", "Washington", "Oregon", "California", "Alaska", "Hawaii", "District of Columbia", "Virgin Islands", "Puerto Rico", "New Mexico", "Pacific Islands", "Mississippi", "Florida", "Puerto Rico", "Arizona", "California", "Texas", "Utah", "New Mexico", "Colorado", "South Carolina", "Louisiana", "Georgia", "Arkansas", "Nevada"}; } int i; for (i = 0; i &lt; areaNumbers.length; i++) if areanumbers[i] == int area; break; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload