File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes help with processing array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with processing array" Watch "help with processing array" New topic
Author

help with processing array

battousai liang
Greenhorn

Joined: Apr 28, 2013
Posts: 6

I am trying to make a method that takes 2 arguments and returns true or false if the parameters of an array are within a certain value.

The programs is a game, were 2 players are given 3 piles of sticks, each pile has a different number of sticks, the player to remove the last stick wins. (so there are only 3 indexes in the array, 0,1,2)

here is the code, the part i am stuck on is at the end of the code, there are notes to explain how the method should work. thanks in advance

edited



Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to that ranch battousai.

Please use code tags when pasting code:



Always write the logic in plain English or your native language first. Something like:


Also, is this the whole class?


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Mansukhdeep Thind wrote: . . .
Always write the logic in plain English or your native language first. Something like:

. . .
Far better way to do it:-reference
Are you supposed to loop through the array? If so, you would require different pseudo‑code.
battousai liang
Greenhorn

Joined: Apr 28, 2013
Posts: 6
The descriptions were not actually written by myself, they are there to help me that is why they are quite long.

So how do i write an 'If' statement to ask if the value inside each index is within it's parameters. for example if the index value is 5, then if i subtracted between 1 - 5 it would be true, if i tried to subtract 8 it would be false

The method has 2 arguments, it;s first asking if the array length is valid from 0 to 2 and then the value inside each index is in range, i hope that makes sense.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

battousai liang wrote:So how do i write an 'If' statement to ask if the value inside each index is within it's parameters.


If it's a small, fixed number of parameters (probably no more than 3 or 4), you can do it as:


Of course "in range" has to be replaced by an appropriate test, such as > 0. And if you have more than a single comparison to make, such as "between two numbers," then you'll have to mention each index multiple times.

You can do it for more than that if it's a fixed number, but the code gets unwieldy. For a large or variables number of items to test, you'd use an array or collection, and a loop. If any one of the items fails its test, you set a flag to indicate that, and break out of the loop.
 
 
subject: help with processing array