Results 1 to 3 of 3
Thread: ArrayList Processing Help!
- 11-21-2012, 01:12 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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.
- 11-21-2012, 02:54 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Re: ArrayList Processing Help!
Actually anything after Washington can't be added
- 11-21-2012, 04:13 AM #3
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
copying contents of an ArrayList to another ArrayList
By ankit1801 in forum New To JavaReplies: 8Last Post: 03-27-2011, 06:07 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Processing help
By menez in forum Java AppletsReplies: 3Last Post: 02-23-2010, 02:13 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks