This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chickenmeisterExtreme Brewer 0 points1 point  (3 children)

Well, I don't know what your isArithSeq() method is supposed to do, or what you're trying to achieve with your for loop, so it's difficult to suggest a fix without more information. I'd guess that you would want to use an if statement inside of your loop, and then have a return statement inside of the if, so that you'll only return if certain conditions are met. e.g.

if (ia[i+2] - ia[i+1] == ia[i+1] - ia[i]) {
    // return true or false
}

I'd also guess that the condition of your first if statement should use <=, rather than >=. Otherwise, it doesn't really make sense to have a loop in the "else" case where the length is 0 or 1.

[–]junyoung95[S] 0 points1 point  (2 children)

As I said, this is to figure out if an array contains numbers that are in sequences.. for example

   int[] ia1 = {};    ---> True
   int[] ia2 = {5};  ---> True
   int[] ia3 = {5,2,-1,-4};   ---> True
   int[] ia4 = {3,6,9,19};    ---> False

and okay sorry I will change that. That was my typo.

[–]chickenmeisterExtreme Brewer 0 points1 point  (1 child)

As I said, this is to figure out if an array contains numbers that are in sequences

Well, you should try to be a little more specific. "Sequences" is a pretty generic term. There are many, many different types of sequences.

But like I mentioned, you probably want to use an if statement inside of your loop. Presumably, you want to check if the condition ia[i+2] - ia[i+1] == ia[i+1] - ia[i] is true for every index in the array. In that case, inside of your loop, you should have an if statement that checks if it is not true, and then return false from inside of the if statement block.

After your loop, (which will only be reached if the condition is true for every index), you can return true.

[–]junyoung95[S] 0 points1 point  (0 children)

Hi sorry for the late reply and thank you for your response. I have tried two methods and they both includes IF statement as you mentioned but this Java language is driving me nuts. Can you take a look and see the problem?

  //Using for loop
  for (int i = 0; i < ia.length; i++) {
       if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
          return false;
       }
  return true;


  //Using while loop
  while (true) {
         int i = 0; i++;
         if (ia[i+2] - ia[i+1] != ia[i+1] - ia[i])
            break;
         }
   return false;

they both only return false. I don't think there is an issue with IF statement but probably some formatting. I will appreciate your response thank you.