Results 1 to 4 of 4
  1. #1
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default A couple of problems trying to get solved

    First of all, does anyone know if there is a problem with the sun forums? For nearly 15 hours there has been 2 members logged in and 123 guest (hasnt changed in all that time), seems a bit strange.

    With my java problems, these one are new and just to fix up my working program. The first one i will show is to do with my array. This is the code affected
    Java Code:
    public class StudentBorrower extends Borrower
    {
    	private static final int MAX_ITEMS = 7;
    	private static final double MAX_FINE = 5.00;
    	private String studentId;
    	private int studentItems=0;
    	
    
    	/**Overridden addItem method from Borrower class*/
    	public void addItem(LibraryItem libraryItem) 
    	{
    		if(studentItems >= MAX_ITEMS) //if student has more items than is allowed
    		{
    			System.out.println("The maximum number of items allowed is 7 " + "\n" + "You currently have " + MAX_ITEMS +
    							   "out at the moment");
    		}
    		
    		if(MAX_FINE > 5.00)  //if student has fines more than 5.00
    		{
    			System.out.println("The maximum fine allowed is 5.00" + "\n" + "You currently have " + MAX_FINE +
    							   "worth of fines");
    		}
    		
    		else
    		{
    			super.addItem(libraryItem);
    			studentItems++;
    		}		
    		
    	}
    When i try to add more than 7 items to my array, i get thrown an error but it also throws a arrayIndexOutOfBounds exception and crashes. For some reason it doesnt seem to break if it throws one of my errors, and it continues to add the item to the array, hence the java error. Am i doing my if and else statements correctly?
    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
      if (studentItems >= MAX_ITEMS) 
      {
        //..
      }
      
      if (MAX_FINE > 5.00)  
      {
        //..
      }
      
      else
      {
        super.addItem(libraryItem);
        studentItems++;
      }
    Which "if" is this "else" attached to? So walk through the logic in your mind. What if you have 20 items but the fine is less than 5. What is going to happen then? What if you used an "else if" at one point in your code?
    Last edited by Fubarable; 01-18-2009 at 04:59 PM.

  3. #3
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default

    oh ok,, so an else is only linked to 1 if, which is why else if is used. And i originally had it like that, but checked it now and it works.

    My second problem was to do with removing an item from the borrowers personal array. I have done it like so
    Java Code:
    	/**Method to remove item from Borrowers array*/
    	public void removeItem(String loanItem)
    	{
    		
    			for (int x = 0; x < itemsBorrowed.length; x++) //loop the array
    			{
    	
    				if(itemsBorrowed[x].title.equalsIgnoreCase(loanItem)) 
    				{
    					itemsBorrowed[x]=null;
    				}
                                           }
    }
    But say i had the items vampire, bfg, the blair witch, scum in my array, and i choose to remove bfg, i would only be left with vampire, meaning that it deleted everything after the element aswell. How can i handle somthing like this? What is the usual way to remove an element? I have searched and see that most people copy the array or somthing, but its quite confusing.
    Any help appreciated.
    Last edited by nick2price; 01-18-2009 at 10:18 PM.

  4. #4
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default

    This is how the method is looking now
    Java Code:
       /**Method to remove item from Borrowers array*/
        public void removeItem(String loanItem)
        {
            int size =  itemsBorrowed.length;
            
                for (int x = 0; x < size; x++) //loop the array
                {
        
                    if(itemsBorrowed[x].title.equalsIgnoreCase(loanItem)) 
                    {
                        for(int k = x; k < size; k++)
                        {
                            itemsBorrowed[k] = itemsBorrowed[k+1];
                             size--;
                        }    
                    }
                    
                                
                }
        }
    So i find the match, start a new loop from that point moving everything up. I am obviously missing somthing, but for the life of me cant get my brain around arrays. Should i also be adding somthing like this in the loop?
    Java Code:
    itemsBorrowed[size] = null;

Similar Threads

  1. Couple of newbie questions
    By ananasman in forum New To Java
    Replies: 11
    Last Post: 11-21-2008, 12:54 AM
  2. new to java get problems solved here !!
    By bill7709 in forum New To Java
    Replies: 5
    Last Post: 10-19-2008, 06:29 PM
  3. Replies: 9
    Last Post: 10-18-2008, 05:31 PM
  4. [SOLVED] Problems with arrays
    By hayjk in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 06:52 PM
  5. Couple of Problems
    By joz_12345 in forum Java 2D
    Replies: 2
    Last Post: 03-06-2008, 05:13 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
  •