-2

edit: the input is always lower as max array (0-50) my main method:

    int [] rndm1 = readUserTip();
    for(int i = 0; i<49;i++)
    {
        System.out.print(rndm1[i]);
    }
    System.out.println();

I try to get this working with a user input but console says always:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:"

I know what the error means but it only happens when i use input method..

static int [] readUserTip() {

    char input;
    int[] readUserTip = new int[50];

    for (int i = 0; i <= 6;) {
        input = In.readChar("Input Number: ");
        readUserTip[input] = 1;
        i++;
    }

realharry fixed it thanks a lot ! char -> int see his answer :)

Brix
  • 11
  • 2

1 Answers1

0

Your code example has a few problems. First, you haven't defined what In is. The variable index is always 0. But, most importantly, the type of input is char, not int, and yet you are trying to use it as an index for an array, which is clearly a NO NO.

The ascii value of the character you input will likely be >= 50, the array size is 50. And, hence you'll likely get ArrayIndex OOB exception. (Most characters below ASCII 50 are special characters, and the values of English alphabets ('A' and up) is 65 or higher.)

realharry
  • 1,435
  • 7
  • 12
  • im sorry "In." is my Input class (in.class) but i'll try it out for changing it to int thanks i understand! Edit: YES you fixed it oh my god im stupid. thanks a lot! Char -> int .. thanks! – Brix Nov 17 '17 at 00:15