Results 1 to 3 of 3
  1. #1
    OmegaDracoMax is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default ArrayList Processing Help!

    The only error I am having now has to do with have an input with the name starting with Z, Aardvark and Java seem to enter fine.

    Adams
    Franklin
    Hale
    Hamilton
    Jefferson
    Madison
    Paine
    Revere
    Washington

    Java Code:
    import java.util.ArrayList ;
    import java.util.Scanner ;
    import javax.swing.JOptionPane ;	
    import java.io.File ;					
    import java.io.IOException ;
    
    /**
     * A class to create and maintain a list of names.
     */
    class NameList
    {
    	// instance var
    	private ArrayList<String> list ;
    	
    	/**
    	 * Creates an empty list.
    	 */
    	public NameList()
    	{
    		list = new ArrayList<String>() ;
    	}
    	
    	/**
    	 * Appends a name to the end of the list.
    	 * @param name the name to be appended
    	 */
    	public void append(String name)
    	{
    		list.add(name) ;		// calling add method of ArrayList class
    	}
    	
    	/**
    	 * Deletes (i.e., removes) the name in element number "index" from the list.
    	 * @param index the position in the list of the name to be deleted.
    	 */
    	public void delete(int index)
    	{
    		list.remove(index) ;	// calling remove method of ArrayList class
    	}
    
    	
    	/**
    	 * Converts the list into a multi-line String.
    	 * @return a String containing all the names on the list, one per line.
    	 */ 
    	public String toString()
    	{
    		String out = "\n" ;	   // initialize string to be returned
    		for (int i = 0 ; i < list.size() ; i++)
    		{
    			out = out + list.get(i) + "\n" ;
    		}
    		return out ;
    	}
    	
    	/**
    	 * Searches for a specific name on the list.
    	 * @param name the name to search for
    	 * @return the index (i.e., "position") of the name on the list, or -1 if
    	 * the name is not on the list.
    	 */
            public int search(String target)
    	{
    		// examine each element in turn...
    		for (int i = 0 ; i < list.size() ; i++)
    		{
    			if (list.get(i).equals(target))	 // found it!
    			{
    				list.remove(i) ;					 // return position
                                    return i;
    			}
    		}
    		// here if target is not on list...return -1
    		return -1 ;						
    	}
    	
    	/**
    	 * Inserts a name in its proper position in a sorted (i.e., "ordered") list.
    	 * @param name the name to be inserted
    	 * Precondition: the list must be sorted in ascending order!
    	 */
    	public String insert(String name)
    	{
    		// write method body here
    		
    		for (int i = 0 ; i < list.size() ; i++)
    		{
    			if (list.get(i).compareTo(name) > 0)	 
    			{
    				list.add(i, name);	
                                    return name;				 
    			}
    		}
    		
    		return name;
    		
    	}
    	
    			  
    }  // end of NameList class definition
    
    public class NameListTester
    {
    	public static void main(String [] args) throws IOException
    	{
    		Scanner infile = new Scanner( new File("sorted.txt") ) ;
    		
    		NameList theList = new NameList() ;
    		
    		String name ;
    		
    		// read the data file until eof and store in theList
    		
    		while ( infile.hasNext() )				// while not eof...
    		{
    			name = infile.nextLine() ;
    			theList.append(name) ;
    		}
    		
    		System.out.println( "The original list:\n" + theList.toString() ) ;
    		
    		// let user enter some names to be removed from the list
    		
    		Scanner scan = new Scanner(System.in) ;
    		
    		name = JOptionPane.showInputDialog(
    			          "Enter a name to be removed (or Cancel to quit)") ;
    		
    		while ( name != null )
    		{
    			
    			// Enter code here to search theList for the name entered.			
    			// If the name was found, remove it from the list; otherwise print
    			// an appropriate message.
    			
    			String target = name;
                            System.out.println();
                                                           
                            int position = theList.search(target);  
                            
                            if ( position >= 0 ) 		// if found...
    			{             
    				System.out.println( target + " found in element " + position ) ;
    			}
    			else						// not found
    			{
    				System.out.println( target + " is not on the list" ) ;
    			}
    			
    			
    			// DO NOT MODIFY OR DELETE THE NEXT TWO LINES
    			System.out.println("\nThe updated list:\n" + theList) ;
    			name = JOptionPane.showInputDialog(
    			          "Enter next name to be removed (or Cancel to quit)") ;
    		}
    		
    		// let user enter some names to be inserted into the list
    		// DO NOT MODIFY THIS CODE
    		
    		name = JOptionPane.showInputDialog(
    			          "Enter a name to be inserted (or Cancel to quit)") ;
    
    		while ( name != null )
    		{
    			theList.insert(name) ;
    			System.out.println("\nThe updated list:\n" + theList) ;
    			name = JOptionPane.showInputDialog(
    			          "Enter next name to insert (or Cancel to quit)") ;
    		}
    	}
    }
    Last edited by OmegaDracoMax; 11-21-2012 at 02:31 AM.

  2. #2
    OmegaDracoMax is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: ArrayList Processing Help!

    Actually anything after Washington can't be added

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Processing Help!

    Any words that are after the last word in the list cannot be inserted because your code attempts to insert the new word BEFORE the existing word. So you have a special case where the new word becomes the last word in the list.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  4. Processing help
    By menez in forum Java Applets
    Replies: 3
    Last Post: 02-23-2010, 02:13 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •