• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in my java code  RSS feed

 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, please, what could be wrong with this java code?

 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno. Why do you think there's anything wrong with it at all?

Does it compile? If not, what errors do you see?
Does it run? If not, what errors do you see?
Does it behave correctly? If not, what happens and what were you expecting?
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

it doesn't compile and this is the eror I got :
int[] move(int[] a, U){
^
1 error
 
Robert Lippens
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the parameters in your move method. Have you appropriately declared your method parameters?
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code modified:



but I got this error:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
henry joe wrote:
but I got this error:



Take a shot at what you think the environment is telling you -- the error message is pretty clear.

Henry
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
certainly the error is clear. Saying I am out of the array length..That is because of my calculation of shifting the elements of the array one step to the right when the second parameter of move is 1:
l[i] = a[(i-U)%(a.length)];

My aim is to shift the element 1 in position 1 in the array n steps to the right where n is the second parameter of the Move() function. My calculation is causing the error.. Any one care to help me out?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
henry joe wrote:
certainly the error is clear. Saying I am out of the array length..That is because of my calculation of shifting the elements of the array one step to the right when the second parameter of move is 1:
l[i] = a[(i-U)%(a.length)];

My aim is to shift the element 1 in position 1 in the array n steps to the right where n is the second parameter of the Move() function. My calculation is causing the error.. Any one care to help me out?


There are two edge conditions that need to be taken care of (when shifting right). The item in the right most position has no place to go, so don't shift it out. And the item in the most left position has nothing to shift into it, so that will be a set operation.

Also, when shifting right, you can't move right -- or all you'll end up with the the left most element copied to every element position.

Henry
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now we're getting somewhere.

What are the rules for your shift (move) function? When things fall off the end of the array are they lost or do they wrap? If they're lost then what do you pad the other end with?

It's quite easy to get yourself in a pickle with these seemingly simple things. I would perhaps recommend you take a moment to sketch out on paper how you would go about doing it if real life. Imagine you have 5 coins in a row, how would you solve it? Then see how you get on with coding it.

We'll wait.
 
Jhon Calado
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

So did you figure out the answer by now?

I was under the impression that this error wouldn't occur since the array would accept the -1 index (considering it the last element of the array).Turns out I'm wrong,but I have seen some object that accepts it.Can anyone recall me which object it is?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!