Out of Bounds!!
hey i cannot get this to work! There method fails if i put a numWanted greater than the number of entries in the log arraylist.
The error message says:
"index out of bounds exception
index: 1, size:1 (in java.util.Arraylist)
An index of somesort is out of range i.e does not exist e.g you have tried to acces element 5, but only 3 elements exist"
Trouble is the specification says that it has to be able to cope with numWanted being greater than the number of entries e.g if i try and access element 5 when there are only 3
If have tried other things and ended up just geting a system output of nothing!
Help would be much appreciated thank you
/** Print a specified number of entries from the log, starting with the
* most recent and working towards earlier entries.
*
* Note: If 'numWanted' is greater than the number of entries in the log,
* the method should NOT fail - it should just print the entire log and
* terminate normally.
*/
publicvoid printLogEntries(int numWanted)
{
while (numWanted>log.size()){
System.out.println();
numWanted--;
}
{
System.out.println(log.get(numWanted));
numWanted--;
}
}
If log.size() == 1 then you should be accessing log.get(0), right?
just put an if statement in there that checks if the input is greater than the size and if so just print out all the elements. its pretty simple if you think about about it for a second.
but i need to print out more than one logwouldnt that just print out the log that was numbered (numWanted) ?
something like this in your code
if (input > arrayList.length)
{
for (int i=0; i<arrayList.length; i++)
{
System.out.println(arrayList(i));
}
}
else
{
for (int i=0; i ><input ; i++)
{
System.out.println(arrayList(i));
}
}
Message was edited by:
kikemelly>
You can also use a try .. catch statement.
try
{
// run through array stuff without worrying too much
}
catch (ArrayIndexOutOfBoundsException e)
{
// what you want your program to do if it attempts to access the array illegally.
}
> something like this in your code
> > if (input > arrayList.length)
> {
> for (int i=0; i<arrayList.length; i++)
> {
> System.out.println(arrayList(i));
> }
> }
> else
> {
> for (int i=0; i <input ; i++)
> {
> System.out.println(arrayList(i));
> }
> }
>
>
> Message was edited by:
> kikemelly
Why duplicate the code? The only thing different between the if and the else, is the upper bound of the loop variable.
Is anyone having trouble with the forum? posts don't display probably, people have stars they shouldn't, I get a "you are not allowed to edit" error also.
> Why duplicate the code? The only thing different> between the if and the else, is the upper bound of> the loop variable.good point.
> catch (ArrayIndexOutOfBoundsException e)
> {
> // what you want your program to do if it attempts
> to access the array illegally. (Most likely this'll
> be blank so that it does nothing ... or maybe output
> an error message to the screen)
Generall a bad idea. That just hides the problem and lets the app go on thinking everything is fine when it's not. Better to either handle the exception--do something to get to a valid state--or wrap and rethrow it, or just don't catch it in the first place.
<code hidden>Message was edited by: DrLaszloJamf
> > catch (ArrayIndexOutOfBoundsException e)
> > {
> > // what you want your program to do if it attempts
> > to access the array illegally. (Most likely
> this'll
> > be blank so that it does nothing ... or maybe
> output
> > an error message to the screen)
>
> Generall a bad idea. That just hides the problem and
> lets the app go on thinking everything is fine when
> it's not. Better to either handle the
> exception--do something to get to a valid state--or
> wrap and rethrow it, or just don't catch it in the
> first place.
I don't think the exercise is about exceptions. I think it means if someone asks for more records than are available then print the most you have without error. just my understanding of the post.
> Generall a bad idea. That just hides the problem and
> lets the app go on thinking everything is fine when
> it's not. Better to either handle the
> exception--do something to get to a valid state--or
> wrap and rethrow it, or just don't catch it in the
> first place.
I was referring mostly to his program .. not under normal circumstances. Of course, checking to make sure you're accessing the array properly in the first place is good coding practice. But catching (and yes, handling) exceptions is also good coding practice.
Why wont this work? it still comes up as an error
im only on a basic level by the way, i am not expected to know anything complicated
/** Print a specified number of entries from the log, starting with the
* most recent and working towards earlier entries.
*
* Note: If 'numWanted' is greater than the number of entries in the log,
* the method should NOT fail - it should just print the entire log and
* terminate normally.
*/
public void printLogEntries(int numWanted)
{
while (numWanted > log.size()) {
System.out.println(log.get(log.size()));
numWanted--;
}
{
System.out.println(log.get(numWanted));
numWanted--;
}
I've already told you why, but let me try again. if log.size()==3, for example, then the entries are at log.get0), log.get(1) and log.get(2). There is no log.get(3), so log.get(log.size()) will always be out of bounds.
i dont know the solution to that
If a list in non-empty, its last entry is log.get(log.size()-1)
i tried that a bit ago and it also would work
>it also would workYou got you code to work?
it came up with the same error and printed the only log (log.size()-1 )times
sorry i meant wouldnt work, im sure i typed wouldnt
Take a look at my reply #10.
why wont this one work?
public void printLogEntries(int numWanted)
{
while ((numWanted > log.size())&&
(numWanted > 0)) {
System.out.println(log.get(numWanted -(numWanted-(log.size()-1))));
numWanted--;
}
{
System.out.println(log.get(numWanted));
numWanted--;
}
Print out numWanted, log.size, numWanted - log.zie() -1 , numWanted - (...), etc. and figure it out.Obivously those numbers don't equal what you think they do. It's an arithmetic error.
jverda at 2007-7-21 17:25:28 >

> System.out.println(log.get(numWanted -(numWanted-(log.size()-1))));
Let's simplify that:
numWanted - (numWanted - (log.size() - 1))
=
numWanted - numWanted + (log.size() - 1)
=
log.size() - 1
Why are you including numWanted in that expression when it just cancels itself out?
eg if number wanted= 7 log size = 4i would want to access 3 i.e log size - 1number wanted - log size - 1 = 4not 37-4 - 1 = 4number wanted - (number wanted - log size - 1) = 3 7- ( 7-4- 1) = 3
> Let's simplify that:Spoilsport. :-P
jverda at 2007-7-21 17:25:28 >

> numWanted - (numWanted - (log.size() - 1))> => numWanted - numWanted + (log.size() - 1)> => log.size() - 1log.size() + 1, not?
jverda at 2007-7-21 17:25:28 >

Were u not taught BODMAS? lol
> 7 - ( 7 - 4 - 1) = 3 You might want to double check your arithmetic there.
jverda at 2007-7-21 17:25:28 >

> > numWanted - (numWanted - (log.size() - 1))
> > =
> > numWanted - numWanted + (log.size() - 1)
> > =
> > log.size() - 1
>
>
> log.size() + 1, not?
No, I still stand by my original. Your questioning it makes me take a second and third look, but it's still right.
its meant to be 7 - (7-(4-1))=3
> > > numWanted - (numWanted - (log.size() - 1))
> > > =
> > > numWanted - numWanted + (log.size() - 1)
> > > =
> > > log.size() - 1
> >
> >
> > log.size() + 1, not?
>
> No, I still stand by my original. Your questioning it
> makes me take a second and third look, but it's still
> right.
Unless I'm miscounting parens or negations, the end result of all that is size + 1.
In any case, your ultimate point stands: Use size - 1 and skip the rest of that krap.
jverda at 2007-7-21 17:25:32 >

Er, what was wrong with my reply #10?
> its meant to be > 7 - (7-(4-1))=3Right, which if you'll notice is the same as (4-1) simplified. The 7 (numWanted value) cancels itself out no matter what.I don't know who you were calling on the BODMAS thing, but I'd look in the mirror if I were you.
> Unless I'm miscounting parens or negations...Sorry, but yes you are miscounting or improperly negating.
> Unless I'm miscounting parens or negations, ...which I guess I was.
jverda at 2007-7-21 17:25:32 >

> Er, what was wrong with my reply #10?Not enough parens and negations. Where's the sexy programmer mystique in something so straightforward. Loser.
jverda at 2007-7-21 17:25:32 >

ok, thing is, it still dontwork
its moaning at me for using a number that is greater than the number entries
but ive got to be able to as you can see:
public class Hotel
{
private String tradingName; // The name by which the guest house is known
private int numRooms;// The number of rooms available to guests
private Room[] rooms;// To hold a Room object for each available room
private int dayNumber;// Starts at 1; should be increased by 1 each day
private ArrayList<String> log; // To hold a note of each event that changes
// the state of any Room object
public Hotel(String nameOfGH, int roomsInGH)
{
rooms = new Room[roomsInGH];
tradingName = nameOfGH;
numRooms = roomsInGH;
log = new ArrayList<String>();
dayNumber=1;
/** Print a specified number of entries from the log, starting with the
* most recent and working towards earlier entries.
*
* Note: If 'numWanted' is greater than the number of entries in the log,
* the method should NOT fail - it should just print the entire log and
* terminate normally.
*/
public void printLogEntries(int numWanted)
//input method here
> ok, thing is, it still dontwork
>
> its moaning at me for using a number that is greater
> than the number entries
> but ive got to be able to as you can see:
Are you saying that you're required to access an index greater than the number of entries? If so, give up now, as it's impossible.
Otherwise, please try to clarify and add details.
jverda at 2007-7-21 17:25:33 >

>ok, thing is, it still dont workWhat was wrong with my reply #10?
yup thats wat they have asked us to do
> yup thats wat they have asked us to doNo, I don't think so. You need to read the assignment and my comments more closely.
jverda at 2007-7-21 17:25:33 >

i done it, well it seems to work!
public void printLogEntries(int numWanted)
{
while ((numWanted > log.size())&&
(numWanted > 0) ){
System.out.println();
numWanted = numWanted -(numWanted - (log.size()-1));
}
{
System.out.println(log.get(numWanted));
numWanted--;
}
This comment is written by the lecturer:
Print a specified number of entries from the log, starting with the
* most recent and working towards earlier entries.
*
* Note: If 'numWanted' is greater than the number of entries in the log,
* the method should NOT fail - it should just print the entire log and
* terminate normally.
> numWanted = numWanted -(numWanted -> (log.size()-1));This is still unnecessarily complex. The two numWanteds cancel each other out. Get rid of them and simplify that expressionn - (n - (s - 1))- ( - (s - 1 )s - 1
jverda at 2007-7-21 17:25:37 >

> This comment is written by the lecturer:
> [code]
> Print a specified number of entries from the log,
> starting with the
> * most recent and working towards earlier
> entries.
>*
> * Note: If 'numWanted' is greater than the number
> of entries in the log,
> * the method should NOT fail - it should just
> print the entire log and
>* terminate normally.
> de]
I know.
And if you read my comment closely, you'll see that what is impossible is to access an element that isn't there, which is NOT what he's asking for.
jverda at 2007-7-21 17:25:37 >

ok calm, im not very good at this stuff,
> ok calm, im not very good at this stuff,I am calm. I'm simply trying to encourage clearer, more precise communcation.
jverda at 2007-7-21 17:25:37 >

Reading this was excruciatingly painful. You guys would have had more success talking to a brick wall.
> Reading this was excruciatingly painful. You guys> would have had more success talking to a brick wall.At least you, myself, and I bet DrLaszloJamf too, feel that way.
I gave solution code in reply #10, but when it just wasn't sinking it, I went back and edited it out. Why bother?