Results 1 to 4 of 4
- 01-18-2009, 03:04 PM #1
Member
- Join Date
- May 2008
- Posts
- 27
- Rep Power
- 0
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
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?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++; } }
thanks
-
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?Java Code:if (studentItems >= MAX_ITEMS) { //.. } if (MAX_FINE > 5.00) { //.. } else { super.addItem(libraryItem); studentItems++; }Last edited by Fubarable; 01-18-2009 at 03:59 PM.
- 01-18-2009, 04:10 PM #3
Member
- Join Date
- May 2008
- Posts
- 27
- Rep Power
- 0
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
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.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; } } }
Any help appreciated.Last edited by nick2price; 01-18-2009 at 09:18 PM.
- 01-18-2009, 09:21 PM #4
Member
- Join Date
- May 2008
- Posts
- 27
- Rep Power
- 0
This is how the method is looking now
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:/**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--; } } } }
Java Code:itemsBorrowed[size] = null;
Similar Threads
-
Couple of newbie questions
By ananasman in forum New To JavaReplies: 11Last Post: 11-20-2008, 11:54 PM -
new to java get problems solved here !!
By bill7709 in forum New To JavaReplies: 5Last Post: 10-19-2008, 05:29 PM -
a couple of questions about Software Engineering plz
By pheonix in forum New To JavaReplies: 9Last Post: 10-18-2008, 04:31 PM -
[SOLVED] Problems with arrays
By hayjk in forum New To JavaReplies: 3Last Post: 04-07-2008, 05:52 PM -
Couple of Problems
By joz_12345 in forum Java 2DReplies: 2Last Post: 03-06-2008, 04:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks