SO I am having much difficulty with the Java code, I am supposed is to modify ContactList.java so that it loops prompting the user for an integer key. If the key is non-negative, search the array for a Person with that key. If the Person is found, display the Person. If the Person is not found, display a message to that effect. If the user enters a negative key, the program should terminate. But not sure where to implement the while Loop. Also Currently run() throws IOException. Im trying change run() so that it catches IOException, and if an exception is caught, display a descriptive message and terminate. I haveto modifying run(), by adding a loop and removing the call to display()!!
import java.io.IOException; import java.net.URL; import java.util.Scanner; public class ContactList { private Person[] theList; private int n; // the number of Persons in theList // Returns a Scanner associated with a specific text-based URL // online. private Scanner makeScanner() throws IOException { final String source = "http://userpages.umbc.edu/~jmartens/courses/is247/hw/05/05.txt"; final URL src = new URL(source); return new Scanner(src.openStream()); } // makeScanner() // Return a Person instance based upon data read from the given // Scanner. private Person getPerson(final Scanner in) throws FileFormatException { if (!in.hasNextLine()) return null; String line = in.nextLine().trim(); int key = Integer.parseInt(line); String name = in.nextLine().trim(); String mail = in.nextLine().trim().toLowerCase(); if (in.hasNextLine()) { String empty = in.nextLine().trim(); // skip blank line if (empty.length() > 0) throw new FileFormatException("missing blank line"); } // if return new Person(key, name, mail); } // getPerson() // Display the array contents. private void display() { for (int i = 0; i < n; ++i) System.out.println(theList[i]); } // display() // Example code to display the contents of the contact list file. private void run() throws IOException { theList = new Person[1024]; Scanner in = makeScanner(); int index = 0; Person p = getPerson(in); while (p != null) { theList[index++] = p; p = getPerson(in); } n = index; display(); } // run() public static void main(String[] args) throws IOException { ContactList cl = new ContactList(); cl.run(); } // main() } // class ContactList