Java issue - help!

Discuss Programming
Post Reply
User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Java issue - help!

Post by Calum » Sun Apr 22, 2007 10:47 am

hi all,

i have a java issue that i need some help with. it's homework, might as well admit it, but i am just after a second pair of eyes really. not so much telling me what to do but maybe pointing out how i should look at something.

i'm not going to post all the code here because i am paranoid about being accused of plagiarism, and wouldn't want to encourage it in others either. So really i am asking for anybody who thinks they might have ten minutes to help to just reply here and i will IM you my issue.

Basically, i am writing the constructor for a class, the two instance variables are a map and a list, which have to be initialised using values from three arrays. Sounds messy, eh? The arrays are two arrays of ints and one array of chars. the list is of "Tile" objects (defined in another class) which have two instance variables, a char and an int (from two of the arrays) and the map has chars as keys and ints from the other int array as the values.

Anyway, you'll see what i mean. Please reply here if you are willing to have a look, but please note that i really am not looking for somebody to do this for me, just to kind of see if you can see where i am going wrong and possibly give me a hint as to what direction to take as i am here with my nose at a wall.

thanks etc.....

EDIT: i think i meant PM, not IM. I meant send it to you via a personal message on this site, or it could be email, whatever. it could be IM i suppose.

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sun Apr 22, 2007 11:43 am

hi all,

well actually i have just figured it all out. i hadn't initialised my instance variables properly and also for some reason this sort of thing works:

Code: Select all

      for (int i = 0 ; i < theInitialNumbers.length ; i++)
(where theInitialNumbers is an array

while this doesn't:

Code: Select all

      for (int i = 0 ; i == 26 ; i++)
still don't know why, but it's working now anyway.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Apr 22, 2007 12:52 pm

The second one doesn't work because you are initializing the variable "i" to 0 and then telling it to loop while "i" is equal to "26" incrementing by "1" each time through the loop. Well, "i" is not equal to 26 from the very beginning so the condition is false and doesn't loop. You have to test for "i" being less than 26 (or less than some variable that is greater than 0) as you did in your first example.

loop 10 times:
i = 0; i < 10; i++
i = 10; i > 0; i--

these will loop 0 times because your condition is false right out of the gate:
i = 0; i > 10; i++
i = 0; i == 10; i++

User avatar
Calum
guru
guru
Posts: 1349
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Tue Apr 24, 2007 5:42 am

oh i see! so that condition is like a "while this, then do this" rather than "do this until this happens" - thanks for that! it all helps!

Post Reply