B
B
bublov2019-04-15 13:10:51
Java
bublov, 2019-04-15 13:10:51

What's wrong with the indexOf method?

The code:

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        while (!(1 <= a && a <= b && b <= Math.pow(10, 6) && b - a <= 3000)) {
            System.out.println("Вы ввели неккоректное значение");
            a = in.nextInt();
            b = in.nextInt();
        }
        int[] masOfNumbers = new int[b-a+1];
        int[] masOfDividers = new int[b-a+1];
        for(int d = 1; d<=(b-a+1); d++){
            masOfNumbers[d-1] = a+d-1;
        }
        for(int c = 1; c<=(b-a+1); c++){
            masOfDividers[c-1] = 0;
        }
        for(int i = a; i<=b; i++){
            for(int j = 1; j<=i; j++){
                if(i%j==0){
                    masOfDividers[i-a]++;
                }
            }
        }
        int max = getMax(masOfDividers);
        String str = Arrays.toString(masOfDividers).replaceAll("\\[|\\]|,|\\s", "");
        System.out.println(masOfNumbers[str.indexOf(max)]);
    }
    public static int getMax(int[] arrayForFindingTheMaximum)
    {
        int maximum = Integer.MIN_VALUE;
        for (int i = 0; i < arrayForFindingTheMaximum.length; i++)
        {
            maximum = Math.max(maximum, arrayForFindingTheMaximum[i]);
        }

        return maximum;
    }
}

The problem lies in the last three lines of the main method.
I checked both variables with a = 1, b =25; max - 8, str - 1223242434262445262644283.
But then why does str.indexOf(max) return -1?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2019-04-15
@bublov

you should use debug to understand how it works

public int indexOf(int ch, int fromIndex) {
        final int max = value.length;
        if (fromIndex < 0) {
            fromIndex = 0;
        } else if (fromIndex >= max) {
            // Note: fromIndex might be near -1>>>1.
            return -1;
        }

        if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
            // handle most cases here (ch is a BMP code point or a
            // negative value (invalid code point))
            final char[] value = this.value;
            for (int i = fromIndex; i < max; i++) {
                if (value[i] == ch) {
                    return i;
                }
            }
            return -1;
        } else {
            return indexOfSupplementary(ch, fromIndex);
        }
    }

Your string str is converted to an array of characters ( final char[] value = this.value;)
and char is a number. Simply put, you compare the character code with your number It's
easy to
test Take your string 1223242434262445262644283 and print indexOf(49) - this is the unit character
Get 0 - the index in the string
public static void main(String[] args) {
        String str = "1223242434262445262644283";
        System.out.println(str.indexOf(50)); //1, потому что '2' == 50
    }

And in order to get the index of a substring, you need to cast max to a string
public static void main(String[] args) {
        String str = "1223242434262445262644283";
        System.out.println(str.indexOf("23")); //2 - здесь работает другая реализация метода indexOf
    }

A
Andrey Goncharov, 2017-02-21
@karjan

full page

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question