• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array problem  RSS feed

 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! For my education I need the method schuifOp to change the given array by an (random 1-4)X amount of places. Thus: if the array(rij3) passed to the method schuifOp is 6 5 2 9 1 and the random number is 1 than the array should end up as 1 6 5 2 9. If the random number is 2 than the array should end up as 9 1 6 5 2. I did already see another method that worked from a classemate. However, I feel like something like this should also work. It does not, however. It keeps returning weird stuff, it does not merely move numbers it seems to make new numbers as well!!:/ Can anyone explain me what is going wrong and or give me some idea of what to change to make it work? Thank you in advance;)

This is the code:

main


method
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and am not seeing random errors. I am seeing an off by one error. You can troubleshoot that with a debugger or by adding System.out.println() statements to see exactly when the problem shows up.



Expected output:
2.2,3.3,4.4,5.5,1.1

Actual ouput:
2.2,3.3,4.4,5.5,5.5

Also, what does this do rij[value-5]? It feels like it makes an assumption about the size of your input.
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply!

The -5 is to make sure it does not go out of bound. If counter = 3 and aantalplaatsen (places) = 3 than element 3 goes to position 6. The array is max 5 elements long. So it goes over the edge --> element out of bound error. I can't find any other solution for this and I am not sure if the current one works....
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you've coded this as you have been trying to work out the solution. I suggest you StopCoding (← click) right now, get a pen and paper and write down, in detail (as if you were explaining it to a 5 year old), how you would do this if someone gave you the input data and told you to produce the desired output. Then, once you have fully described the solution, and only then think about converting that to code.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dirk Lucas wrote:The -5 is to make sure it does not go out of bound.

So why 5? Why not rij.length? That way you still have the guard when the array is a different length.
 
Dirk Lucas
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, the stop coding thing I did see before, and it is one of my big problems as I stare to much and to long at my screen when stuck. (So I try to think of your advice when it happens again;))

In this case, however, I know there is another way to do it ( I saw the code of a fellow student). But I just felt this(my own method up there) way should be "workable" as well if I'd understand arrays correctly.

And seeing Jeanne's replies I believe it should work indeed. So I am glad I was on the right track afterall. I ll give it another try and report back here!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!