avihu24
avihu24

Reputation: 53

how to fix a method that works only with even indexes?

i wrote this method that checks whether there's a digit that is contained inside a next array's index's number , if there is at least 1 digit that is a part of the next number , it will go in a counter and if the counter's value is more than 0 it will go inside a variable "check" that will be equal true.
it's enough to have 1 index that is false to make the whole array false. they all have to be true;

for example an array that is correct according to the method i built: int [] arr = {12,527,71,114,49,1902};

thats for the method explaintion. now for my problem. the method works only with even numbers and i dont know what could go work. thanks to all the helpers!

public static boolean checkSpecialArray(int[] arr)
    {
        int i = 0;
        int j = 0;
        int x = 0;
        boolean check = true;
        int counter = 0;
        for (i = 0; i < arr.length; i++)// array's length
        {String Dig = Integer.toString(arr[i++]);//next array's number
        String num = Integer.toString(arr[i]);//first array's number
            for (j = 0; j < num.length(); j++)// check first array's index number
                for (x = 0; x < Dig.length(); x++)// check 2nd array's index number
                    if (Integer.parseInt("" + num.charAt(j)) == Integer.parseInt("" + Dig.charAt(x)))//checks a digit to another digit
                        counter++;
                    
            if(counter>0)
            check = true;
                else if(counter==0)
                 return false;
            
            counter=0;  
        }
        if(check)
            return true;
        
        return false;
    } 

Upvotes: 0

Views: 68

Answers (1)

Thomas
Thomas

Reputation: 88717

I'd have to guess here but I assume this is the source of your problem:

 for (i = 0; i < arr.length; i++) { // array's length 
   String Dig = Integer.toString(arr[i++]);//next array's number

Here you are incrementing i inside the loop as well so it skips numbers. You'd probably want to do Integer.toString(arr[i + 1]) instead.

Note that i++ basically is the same as i += 1 or i = i + 1, so having that in your loop's "next" operation (3rd part of the for-construct) as well as in the body basically makes the loop body start with indices 0, 2, 4, etc. which seems to only use even indices.

Also, having String num = Integer.toString(arr[i]); at the second line basically uses the "next" number and not the first.

Finally i < arr.length; would cause problems for arrays of odd length because in the body you'd try to access a non-existing index at the end (due to that i++ but even a i + 1 would cause this).

Why do you need that (as per the question in your comment)?

Since you are trying to compare numbers at indices i and i + 1 you need to make sure that i doesn't "leave" the array, i.e. the maximum allowable index would be array.length - 2. This is because indices are zero-based so the last allowable index for an array would be array.length - 1 (e.g. 4 in an array of length 5).

Now let's consider the options for the loop condition:

  • i<arr.length - this would allow i to refer to the last index in the array and thus arr[i + 1] would be out of bounds, hence the exception you got
  • i<=arr.length - this would allow i to get even more out of bounds and arr[i] would fail already
  • i<arr.length-1 - this would restrict i to only refer indices up to the second-to-last (e.g. index 3 in an array of length 5) and hence arr[i + 1] would still be within bounds and refer to the last element.

So you'd want to have the loop look like this:

//from first to second-to-last index
for (i = 0; i < arr.length - 1; i++) {
  String num = Integer.toString(arr[i]);//first array's number

  //also note the lowercase variable name which is more according to the Java code conventions
  String dig = Integer.toString(arr[i+1]);//next array's number

Alternatively you could use an enhanced for loop, e.g. like this:

 String prev = null;

 for( Integer num : arr ) {
   //or use Math.abs(num) to also be able to handle negative integers easily
   String next = Integer.toString(num);

   //only if there was a previous number
   if( prev != null ) {
     //compare digits/characters here
   } 

   //keep reference to the "current" number in string form to act as previous in the next iteration
   prev = next;
 }

Upvotes: 2

Related Questions