Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; import java.io.InputStream; import java.io.IOException

ID: 3605092 • Letter: I

Question

import java.util.Scanner;

import java.io.InputStream;

import java.io.IOException;

import java.net.URL;

import java.net.URLConnection;


/**

* Code for HW7

* @author

*/

/**

   This program provides lookups by city and zip code

*/

public class ZipLookupWeb

{

   public static void main(String[] args)

   throws IOException

   {

Scanner in = new Scanner(System.in);

boolean more = true;

while (more){

System.out.println("Lookup Z)ip, C)ity name, Q)uit?");

String cmd = in.nextLine();

if (cmd.equalsIgnoreCase("Q")){

more = false;

}else if (cmd.equalsIgnoreCase("Z")){

System.out.println("Enter Zipcode:");

String n = in.nextLine();

String pageurl = ...

URL u = new URL(pageurl);

URLConnection connection = u.openConnection();

InputStream inStream = connection.getInputStream();

Scanner ins = new Scanner(inStream);

String statename = "";

String cityname = "";


// read each line, detect if the line contains city name or state name,

// extract and store the information

while (...){

...

}

System.out.println(cityname + ", " + statename);

ins.close();

}else if (cmd.equalsIgnoreCase("C")){

System.out.println("Enter city name:");

String ct = in.nextLine();

System.out.println("Enter State name:");

String st = in.nextLine();


String pageurl = ...

URL u = new URL(pageurl);

URLConnection connection = u.openConnection();

InputStream inStream = connection.getInputStream();

Scanner ins = new Scanner(inStream);

// read each line, detect if the line contains zip code,

// extract zip code and print

while (...)

{

...

}

ins.close();

}

} // end while

in.close();

   }

}

Complete the program that shows (i) zip codes given a city and (ii) city given a zip code at the URL http://www.melissadata.com/lookups/zipcityphone.asp. In the web site (1) When you enter a zip code, the URL returns a page with information about the zip code (city, state, population, and number of business, etc. in the zip code). For example, if you enter 50011, the returned page is http://www.melissadata.com/lookups/ZipCityPhone.asp?InData 50011 and you can check the corresponding city is Ames, IA. (2) When you enter a city name (and a state name), the URL returns a page with information about the city (list of zip codes, are codes, etc.). For example, if you enter "Ames IA", the return page is http://www.melissadata.com/lookups/ZipCityPhone.asp?InData amestia and you can check the list of zip codes and area codes in Ames, IA. In the program, use the menu of ZipLookup.java from Homework 6. In other words, ask the user to select (i) lookup by zip, (ii) by city, (ii) or quit. You can assume user will give five digits for lookup by zip code, and city and state (abbreviated) names for lookup by city. Unlike Homework 6, your program should list all zip codes for lookup by city. After user selects a menu, send the request to the server using the URLConnection. Get the information needed from the HTML in the web page. In Appendices A and B at the end of this document, screenshot and sample HTML codes are attached for lookup by zip (Appendix A) and lookup by city (Appendix B).

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse, select code and press Ctrl+a and then Ctrl+i.
Please don't forget to rate the answer if it helped. Post a comment in case of any issues.




import java.util.Scanner;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
/**
* Code for HW7
* @author
*/
/**
This program provides lookups by city and zip code
*/
public class ZipLookupWeb
{  
public static void main(String[] args)
throws IOException
{

Scanner in = new Scanner(System.in);
boolean more = true;
while (more){
System.out.println("Lookup Z)ip, C)ity name, Q)uit?");
String cmd = in.nextLine();
  
if (cmd.equalsIgnoreCase("Q")){
more = false;
}else if (cmd.equalsIgnoreCase("Z")){
System.out.println("Enter Zipcode:");
String n = in.nextLine();
String pageurl = "http://www.melissadata.com/lookups/ZipCityPhone.asp?InData=" + n;
URL u = new URL(pageurl);
URLConnection connection = u.openConnection();

InputStream inStream = connection.getInputStream();
Scanner ins = new Scanner(inStream);

String statename = "";
String cityname = "";
// read each line, detect if the line contains city name or state name,
// extract and store the information
while (ins.hasNextLine()){  
String line = ins.nextLine();
int idx1, idx2;
if( line.indexOf("<td class='columresult'>State</td>") != -1)
{
idx1 = line.indexOf("<b>");
idx2 = line.indexOf("</b>");
statename = line.substring(idx1 + 3, idx2).trim();
}
else if(line.indexOf("<td class='columresult'>USPS Preferred City Name</td>") != -1)
{
idx1 = line.indexOf("<b>");
idx2 = line.indexOf("</b>");
cityname = line.substring(idx1 + 3, idx2).trim();
}

}
System.out.println(cityname + ", " + statename);
ins.close();

}else if (cmd.equalsIgnoreCase("C")){
System.out.println("Enter city name:");
String ct = in.nextLine();
System.out.println("Enter State name:");
String st = in.nextLine();
String pageurl = "http://www.melissadata.com/lookups/ZipCityPhone.asp?InData=" + ct + "+" + st;
  
URL u = new URL(pageurl);
URLConnection connection = u.openConnection();

InputStream inStream = connection.getInputStream();
Scanner ins = new Scanner(inStream);
  
// read each line, detect if the line contains zip code,
// extract zip code and print
while (ins.hasNextLine())
{  
String line = ins.nextLine();
int idx1 = line.indexOf("zip=");
int idx2 ;
if(idx1 != -1)
{
idx2 = line.indexOf('"', idx1);
System.out.println(line.substring(idx1 + 4, idx2).trim());
}
}
ins.close();
}
} // end while
  
in.close();
}
}



output
======
Lookup Z)ip, C)ity name, Q)uit?
C
Enter city name:
Ankeny
Enter State name:
IA
50021
50023
Lookup Z)ip, C)ity name, Q)uit?
z
Enter Zipcode:
50306
DES MOINES, Iowa
Lookup Z)ip, C)ity name, Q)uit?
q