Create a class: Reader
Start with the code here: This uses the scanner class to read in
from here.
We want it to finish the method findCity that will take
in a zip code and return the city,state as a string where the zip
code is located.
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class ZipCodeReader {
public static String findCity(int zipCode)
{
try {
URLConnection connection =
new URL("http://www.neurophys.wisc.edu/zipcodes.txt").openConnection();
Scanner urlScan=new Scanner(connection.getInputStream());
while (urlScan.hasNext())
{
String line = urlScan.nextLine();
System.out.println(line);
}
return "you will need to write the code to find the city and state based on " + zipCode;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(ZipCodeReader.findCity(10310));
}
}
Afterwards in a class called ScannerExercises
- Ask for their zip code and print out they live in this city,state.
- Create a method readPhoneNumber() that will read in a persons
area code, then their 7 digit phone number and print it out as
such (718)981-9402
- Create a method called findMagicLetter that will ask the user
for their name and print randomly one letter in there name. (Ie
Jeff - it might print out Your magic letter is f )
|