Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Having Trouble Modify the code!!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having Trouble Modify the code!!

    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
    Last edited by jehov86; May 2nd, 2012 at 11:09 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,143
    Thanks
    48
    Thanked 2,157 Times in 2,130 Posts

    Default Re: Having Trouble Modify the code!!

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Having Trouble Modify the code!!

    Recommended reading: How to Program (stuck on homework? READ THIS BEFORE POSTING)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Beginner having some trouble with two parts in my code
    By csprung in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 04:23 AM
  2. Replies: 1
    Last Post: April 17th, 2012, 06:21 AM
  3. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  4. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 07:54 PM
  5. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM