-2

I'm attempting to create a new array of indexes from str2 parameter, but getting this error: "Array required, but string found." I'm learning Java, and only comfortable writing in Javascript. Could someone explain what this error message means?

public class Scramblies {

    public static boolean scramble(String str1, String str2) {
       String alphabet = "abcdefghijklmnopqrstuvwxyz";
       int[] inOfStr2Nums = new int[str2.length()];


       for (int i = 0; i < str2.length(); i++){
          inOfStr2Nums[i] = alphabet.indexOf(str2[i]);     
        }
         System.out.println(inOfStr2Nums);

    }

}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Andrew Lastrapes
  • 187
  • 3
  • 15
  • 1
    You can't access a character in a string using `str2[i]`. That operation only works for an array. – khelwood Oct 29 '18 at 14:57
  • 1
    Also, you'll want `println(Arrays.toString(inOfStr2Nums)) ` – OneCricketeer Oct 29 '18 at 14:58
  • str[i] is not a valid way to get the character out of string. You can convert your string to character array, and then iterate over it: public static boolean scramble(String str1, String str2) { String alphabet = "abcdefghijklmnopqrstuvwxyz"; int[] inOfStr2Nums = new int[str2.length()]; char[] str2Array = str2.toCharArray(); for (int i = 0; i < str2Array.length; i++) { inOfStr2Nums[i] = alphabet.indexOf(str2Array[i]); } System.out.println(inOfStr2Nums); return false; } – G.G. Oct 29 '18 at 15:04

1 Answers1

-1

To fix the error:

inOfStr2Nums[i] = alphabet.indexOf(str2.charAt(i));
jurez
  • 4,436
  • 2
  • 12
  • 20