First you’ll create a list of zip codes, stored as values of array elements, as follows:
Ask for the quantity of 5-digit zip codes you’ll need. Allow the user to enter 0 as well. Save to a variable.
i.Use try-catch to make sure that an integer is entered. Loop back and make the user try again until an integer is entered.
Ask for that many 5-digit zip codes (perhaps you’ll use a For loop). Make sure that the user types in 5 digits. Make them try again and again until they give a 5-digit value.
i.Of course, if the user entered 0 in step a, your program won’t ask for any 5-digit zip codes.
Save each zip code as an array element. I would probably make the array’s data type be string (giving you a length property to work with when you’re checking to make sure it’s 5 digits).
Now, as long as your quantity wasn’t 0, you have the array that holds a varying quantity of zip codes of areas to which this company makes deliveries.
If the quantity is not 0, prompt the user to enter a 5-digit zip code. Make them enter 5 digits – and repeat if they don’t get it right. Save the 5-digit zip code to a variable.
If the quantity was not 0, have your code check to see if the zip code entered in at step #3 exists in the array of available zip codes (created in step #1).
If the zip code entered in at step #3 exists in the array, set a boolean variable named zipExists (or something like that) equal to true.
If the zip code entered does not exist in the array, set the boolean variable equal to false.
If the quantity was not 0, and If the boolean variable set in step #4 is true, display a message to the screen indicating that the zip code exists. Make sure that the message includes the zip code that was keyed in at step #4.
If the boolean variable = false, display a message, including the zip code in the message, saying that the zip code doesn’t exist in the list.
If, on the other hand, steps 3-5 never happened because the quantity was 0, write a message stating that there are not zip codes to validate against.
Expert Answer
import java.io.*;
import java.util.*;
public class HelloWorld{
public static void main(String []args){
int quantityOfZipCodes;
Scanner sc=new Scanner(System.in);
//Loop will run unitl user enters valid value.
while(true){
try{
quantityOfZipCodes=sc.nextInt();
System.out.println(“Enter the quantity of zip codes: “+quantityOfZipCodes);
break;
}catch(InputMismatchException e){
System.out.println(“Please enter a valid number!”);
}
}
//String array of zip codes of length quantityOfZipCodes.
String[] zipCodes=new String[quantityOfZipCodes];
if(quantityOfZipCodes>0){
//Iterate loop till quantityOfZipCodes
for(int i=0;i<quantityOfZipCodes;i++){
System.out.print(“Enter 5-digit zip code: “);
zipCodes[i]=sc.next();
System.out.println(zipCodes[i]);
//Iterate loop till user enters a zip code of length 5
while(zipCodes[i].length()!=5){
System.out.print(“please enter a valid 5-digit zip code: “);
zipCodes[i]=sc.next();
System.out.println(zipCodes[i]);
}
}
}
String zipCode;
Boolean zipExists=false;
if(quantityOfZipCodes>0){
//Ask user to enter a zip code to match.
System.out.println(“Enter a 5-digit zip code: “);
zipCode=sc.next();
System.out.println(zipCode);
while(zipCode.length()!=5){
System.out.print(“please enter a valid 5-digit zip code: “);
zipCode=sc.next();
System.out.println(zipCode);
}
//Match zipcode against available zipcodes. If finds the zipcode, assign zipExists to true and exit the loop.
for(int i=0;i<quantityOfZipCodes;i++){
if(zipCode.equals(zipCodes[i])){
zipExists =true;
break;
}
}
if(zipExists){
System.out.print(“zip code “+zipCode+” exists”);
}else{
System.out.print(“zip code “+zipCode+” doesn’t exists”);
}
}else{
System.out.print(“there are no zip codes to validate against.”);
}
}
}
OUTPUT-