Results 1 to 4 of 4

Thread: Removing Method

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Removing Method

    Java Code:
    //********************************************************************
    //  CDCollection.java       Author: Lewis/Loftus
    //
    //  Represents a collection of compact discs.
    //********************************************************************
    
    import java.text.NumberFormat;
    
    public class CDCollection
    {
       private CD[] collection;
       private int count;
       private double totalCost;
    	private CD[] temp;
    
       //-----------------------------------------------------------------
       //  Constructor: Creates an initially empty collection.
       //-----------------------------------------------------------------
       public CDCollection ()
       {
          collection = new CD[100];
          count = 0;
          totalCost = 0.0;
    		[B]
       }///start method
    	
    	public String removeCd(String art) {
    	 int i =0;
    	 while (!collection[i].equals(art)){
    	 i++;
    	 }
    	 
    	 int j = 0;
    	 temp= new CD[collection.length-1];
    	 for ( int j = 0; j< collection.length-1; j++) {
    	 		collection[j]=temp[j]) 
    			return System.out.println(collection[j]);
    			
    			[/B]
    			
    	 }
    	 
    	 
    	}// Trying to make a remove method that takes deletes a cd and shifts the space(The part in bold)...

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Unless the array is some sort of requirement, if you're doing something that has a lot of add, remove and lookup commands, I'd suggest using one of the List implementations, ArrayList for example. Look it up.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Can anyone tell me why im getting a null pointer exception plzz b4 12 a.m..
    Java Code:
    //********************************************************************
    //  CDCollection.java       Author: Lewis/Loftus
    //
    //  Represents a collection of compact discs.
    //********************************************************************
    
    import java.text.NumberFormat;
    
    public class CDCollection
    {
       private CD[] collection;
       private int count;
       private double totalCost;
    	private CD[] temp;
    
       //-----------------------------------------------------------------
       //  Constructor: Creates an initially empty collection.
       //-----------------------------------------------------------------
       public CDCollection ()
       {
          collection = new CD[100];
          count = 0;
          totalCost = 0.0;
    		
       }///s[B]tart method
    	
    	public void removeCD(String title) {
    	 int i =0;
    	 while (!collection[i].equals(title));
    	 {
    				 i++;
    	 for (int g=i; g<collection.length-1;g++){
    	 		if (g==collection.length-1) {
    				collection[collection.length -1] =null;
    				 } else {
    				 collection[g] = collection[g+1];
    				 }
    	 			}
    	 int j = 0;
    	 temp= new CD[collection.length-1];
    	 for ( j = 0; j< collection.length-1; j++) {
    	 		collection[j]=temp[j] ;
    			}
    	 	}
    	 }
    	[/B]
       //-----------------------------------------------------------------
       //  Adds a CD to the collection, increasing the size of the
       //  collection if necessary.
       //-----------------------------------------------------------------
       public void addCD (String title, String artist, double cost,
                          int tracks)
       {
          if (count == collection.length)
             increaseSize();
    
          collection[count] = new CD (title, artist, cost, tracks);
          totalCost += cost;
          count++;
       }
    
    
    [CODE]//********************************************************************
    //  Tunes.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of an array of objects.
    //********************************************************************
    
    public class Tunes
    {
       //-----------------------------------------------------------------
       //  Creates a CDCollection object and adds some CDs to it. Prints
       //  reports on the status of the collection.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          CDCollection music = new CDCollection ();
    
          music.addCD ("Storm Front", "Billy Joel", 14.95, 10);
          music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
          music.addCD ("Soundtrack", "Les Miserables", 17.95, 33);
          music.addCD ("Graceland", "Paul Simon", 13.90, 11);
    	  music.removeCD("Double live");
          System.out.println (music);
    
          music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
          music.addCD ("Greatest Hits", "Jimmy Buffet", 15.95, 13);
    
          System.out.println (music);
       }
    }
    //-----------------------------------------------------------------
    // Returns a report describing the CD collection.
    //-----------------------------------------------------------------
    public String toString()
    {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n ";
    report += "My CD Collection\n\n";

    report += "Number of CDs: " + count + "\n";
    report += "Total cost: " + fmt.format(totalCost) + "\n";
    report += "Average cost: " + fmt.format(totalCost/count);

    report += "\n\nCD List:\n\n";

    for (int cd = 0; cd < count; cd++)
    report += collection[cd].toString() + "\n";

    return report;
    }

    //-----------------------------------------------------------------
    // Increases the capacity of the collection by creating a
    // larger array and copying the existing collection into it.
    //-----------------------------------------------------------------
    private void increaseSize ()
    {
    CD[] temp = new CD[collection.length * 2];

    for (int cd = 0; cd < collection.length; cd++)
    temp[cd] = collection[cd];

    collection = temp;
    }
    }
    [/CODE]

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
    temp= new CD[collection.length-1];
    	 for ( j = 0; j< collection.length-1; j++) {
    	 		collection[j]=temp[j] ;
    			}
    	 	}
    	 }
    Your temp array is one element shorter than the collection array, so the last iteration of the for loop spawns the exception.
    Also, did you think about my suggestion on using a List?
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Removing a JButton
    By Lingerz in forum New To Java
    Replies: 15
    Last Post: 01-10-2010, 01:18 AM
  2. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 02:03 PM
  3. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 08:23 AM
  4. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 06:56 PM
  5. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 12:46 PM

Posting Permissions

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