Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

public ListItem getnth (int n) { if (getNumItems () == 0) { return null; } else { for (int i = 1 ; i == n ;) { Current = Head; Current = Current.getNext (); i++; } return Current; } } } I wrote this method to get nth item in queue but its not working properly

OpenStudy (anonymous):

Move Current = Head outside of the loop

OpenStudy (anonymous):

it's outside

OpenStudy (anonymous):

oh wait lol yh i was checking to see if tht was the problem but it still isnt working

OpenStudy (anonymous):

would u like me to send u the whole code?

OpenStudy (anonymous):

thts the listitem class file

OpenStudy (anonymous):

thats the queue file

OpenStudy (anonymous):

thts the test class file

OpenStudy (anonymous):

if u have a ready compiler u can run it

OpenStudy (anonymous):

http://compsci.ca/holtsoft/ready171_install.exe or u can get it here

OpenStudy (anonymous):

What's not working?

OpenStudy (anonymous):

the base file

OpenStudy (anonymous):

i think thts all the files might have pasted one twice not sure

OpenStudy (anonymous):

Describe what you're trying to do, what you expect to happen, and what actually happens.

OpenStudy (anonymous):

im trying to access nth item in a list

OpenStudy (across):

shouldn't for "(int i = 1 ; i == n ;)" be "for (int i = 1 ; i <= n ;)" ?

OpenStudy (anonymous):

lemme try tht, crap, wait how do exit conditions work in java, still trying to learn language

OpenStudy (anonymous):

Good catch, across

OpenStudy (anonymous):

lol thnkx

OpenStudy (anonymous):

so for loops the thing will iterate until out of bounds

OpenStudy (anonymous):

lol fail error thnkx guys

OpenStudy (across):

That's correct. for(int i = 0; i < 10; i++); That loop will iterate 10 times before terminating. And you're welcome.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!