Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unable to add object to array.

    When trying to add person to my list I get the cannot find symbol.

    Code:
    import java.util.Scanner;
    import java.io.*;
    import list.*;
    
    public class TestSalesReport 
    {
    	SortedList list = new SortedList();
    	
    	static int salesTotal;
       static float expensesTotal;
    	
    	// Scanner based constructor.
    	public TestSalesReport(Scanner inFile) 
    	{
    		SalesPerson person;
    		String name;
    		int sales;
    		float expenses;
      	
    		while(inFile.hasNext())
    		{
    			name = inFile.nextLine();
    			sales = inFile.nextInt();
    			expenses = inFile.nextFloat();
    			person = new SalesPerson(name, sales, expenses);
    			list.add(person);
    			salesTotal = salesTotal + sales;
    			expensesTotal = expensesTotal + expenses;
    		}
       
    	}
    }
    My sorted list class:

    Code:
    package list;
    
    //------------------------------------------------------------------------//
    // class SortedList                                                       //
    // Implements a sorted list                                               //
    //------------------------------------------------------------------------//
    public class SortedList extends List
    {
    
    //------------------------------------------------------------------------//
    // SortedList()                                                           //
    // Defalut Constructor. Calls parent class                                //
    //------------------------------------------------------------------------//
      public SortedList()
      {
        super();
      }
    
    //------------------------------------------------------------------------//
    //  public SortedList(int)                                                //
    //  Constructor. Calls corresponding constructor in parent class          //
    //------------------------------------------------------------------------//
      public SortedList(int maxItems)
      {
        super(maxItems);
      }
    
    //------------------------------------------------------------------------//
    // boolean isThere(Comparable)                                            //
    // Returns true if the specified item is in the list...otherwise false    //
    //------------------------------------------------------------------------//
      public boolean isThere(Comparable item)
      {
        int first = 0;
        int last = numItems-1;
        int middle;
        boolean found = false;
        while (last >= first && !found)
        {
          middle = (first + last)/2;
          if (item.compareTo(listItems[middle])==0)
            found = true;
          else if (item.compareTo(listItems[middle])<0)
            last = middle -1;
          else
            first = middle + 1;
        }
        return found;
      }
    
    //------------------------------------------------------------------------//
    // void delete(Comparable)                                                //
    // Removes the specified item from the list                               //
    //------------------------------------------------------------------------//
      public void delete(Comparable item)
      {
        int index = 0;
        boolean found = false;
        while (index < numItems && !found)
        {
          if (listItems[index].compareTo(item) == 0)
            found = true;
          else
            index++;
        }
        if (found)
        {
          for (int count = index; count < numItems - 1;count++)
            listItems[count] = listItems[count+1];
          numItems--;
        }
      }
    
    //------------------------------------------------------------------------//
    // void insert(Comparable)                                                //
    // Inserts the specified item into the list                               //
    //------------------------------------------------------------------------//
      public void insert(Comparable item)
      {
        if (!isFull())
        {
          int index = numItems - 1;
          while (index >= 0 && (item.compareTo(listItems[index]) < 0))
          {
            listItems[index+1] = listItems[index];
            index--;
          }
          listItems[index+1] = item;
          numItems++;
        }
      }
    }

    My initial list class:

    Code:
    package list;
    
    //------------------------------------------------------------------------//
    // class List                                                             //
    // Provides routines needed for implementing a generic list               //
    //------------------------------------------------------------------------//
    public abstract class List
    {
      protected Comparable[] listItems;
      protected int numItems;
      protected int currentPos;
    
    //------------------------------------------------------------------------//
    // List()                                                                 //
    // Default Constructor. Creates an empty list of size 100                 //
    //------------------------------------------------------------------------//
      public List()
      {
        numItems = 0;
        listItems = new Comparable[100];
        currentPos = 0;
      }
    
    //------------------------------------------------------------------------//
    // List(int)                                                              //
    // Constructor. Creates an empty list of the specified size               //
    //------------------------------------------------------------------------//
      public List(int maxItem)
      {
        numItems = 0;
        listItems = new Comparable[maxItem];
        currentPos = 0;
      }
    
    //------------------------------------------------------------------------//
    // boolean isFull()                                                       //
    // Returns true if the list is full...otherwise false                     //
    //------------------------------------------------------------------------//
      public boolean isFull()
      {
        return (listItems.length == numItems);
      }
    
    //------------------------------------------------------------------------//
    // boolean isEmpty                                                        //
    // Returns true if the list is empty...otherwise false                    //
    //------------------------------------------------------------------------//
      public boolean isEmpty()
      {
        return (numItems == 0);
      }
    
    //------------------------------------------------------------------------//
    // int length()                                                           //
    // Returns the number of items in the list                                //
    //------------------------------------------------------------------------//
      public int length()
      {
        return numItems;
      }
    
    //------------------------------------------------------------------------//
    // void resetList()                                                       //
    // Sets the position of the list to the beginning for use with the        //
    // getNextItem() function                                                 //
    //------------------------------------------------------------------------//
      public void resetList()
      {
        currentPos = 0;
      }
    
    //------------------------------------------------------------------------//
    // Comparable getNextItem()                                               //
    // Returns the item pointed to by currentPos...increments currentPos when //
    // done                                                                   //
    //------------------------------------------------------------------------//
      public Comparable getNextItem()
      {
        Comparable next = listItems[currentPos];
        if (currentPos == numItems -1)
          currentPos = 0;
        else
          currentPos++;
        return next;
      }
    
    //------------------------------------------------------------------------//
    // void delete(Comparable)                                                //
    // Removes the specified item from the list                               //
    //------------------------------------------------------------------------//
      public abstract void delete(Comparable item);
    
    //------------------------------------------------------------------------//
    // void insert(Comparable)                                                //
    // Inserts the specified item into the list                               //
    //------------------------------------------------------------------------//
      public abstract void insert(Comparable item);
    
    //------------------------------------------------------------------------//
    // isThere(Comparable)                                                    //
    // Returns true if the specified item is in the list otherwise returns    //
    // false                                                                  //
    //------------------------------------------------------------------------//
      public abstract boolean isThere(Comparable item);
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Works fine for me, except you should trim any input off of Scanner with string values, otherwise comparisons may not be quite what you want if you leave preceding whitespace.
    You'll need to be more specific on your error; since you didn't provide SalesPerson I wrote one to match the definitions you have here, and since mine works fine with what you have here I'd suspect that SalesPerson is where your error is.

    I'm curious why you chose to write your own abstract List instead of either implementing java.util.List or extending java.util.AbstractList? Writing your own lists will require the writing of the iterators as well in order to use them within any looping structure. It should also be generic specifying Comparable as its type T during construction.


  •  

    Posting Permissions

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