-1
package Captain_Ship.alphanum;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;

public class A2N {
    public static String main(String input) {
        char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z'};
        String output = "";
        char[] input_char = input.toLowerCase().toCharArray();
        int[] output_int = {};

        //Still doesn't work. outputs [I@123772c4 in place of 8 5 12 12 15
        for (int i = 0; i>input_char.length; i++) {
            output_int[i] = ArrayUtils.indexOf(alphabet,input_char[i]);
        }
        output = Arrays.toString(output_int);
        return output;
    }
}

This is my code. It is super simple with its goal. Take a sentence and translate each letter into a number. So A would be 1, B would be 2, etc. I have tried everything to try and get the loop to work as I want it to. This piece of code is currently the only version which gave me something other than something similar to this: [I@123772c4. I have run out of the options I have researched.

3 Answers3

0
  1. Use a List instead of an array if you do not know the size of the array e.g. you do not know the size of output_int and therefore it's always better to use a dynamic array kind of object e.g. List.
  2. You can use a List for storing the characters as well. List is part of Java collection API and this way you do not have to use a 3rd party library like org.apache.commons.lang3.ArrayUtils for operations like finding the index of an element.
  3. Your loop should check i < input_char.length instead of i > input_char.length which will never be true.

Demo:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // List of alphabets
        List<Character> alphabet = List.of('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
                'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');

        // A sample string
        String input = "sample string";

        char[] input_char = input.toLowerCase().toCharArray();

        // List to store the index of each character of `input` in `alphabet`. Note: if
        // a character is not found, the indexOf method returns -1
        List<Integer> output_int = new ArrayList<>();

        for (int i = 0; i < input_char.length; i++) {
            output_int.add(alphabet.indexOf(input_char[i]));
        }

        // Display the list
        System.out.println(output_int);
    }
}

Output:

[18, 0, 12, 15, 11, 4, -1, 18, 19, 17, 8, 13, 6]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

for (int i = 0; i>input_char.length; i++) { would never execute because i is never greater than input_char.length when initialized to 0.

So change it to for (int i = 0; i < input_char.length; i++) {

And then initialize output_int to a char array of length equal to input_char.length.

int[] output_int = new int[input_char.length].

Don't name your method main, although it compiles - never advisable. main is used as entry point of your application.

SomeDude
  • 13,876
  • 5
  • 21
  • 44
0

The indexOf() method returns index of given character value or substring. If it is not found, it returns -1.


public class Main {

public static void main(String[] args) {


    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    Map<Integer, String> map = new HashMap<>();
    map = alphabetFrom0Index(alphabet);
//        printIndexAndLetters(alphabetFrom0Index(alphabet));

    String test = "Does it work";
    String test2 = "abc xyz";
    System.out.println(returnIndexNumbers(map, test));  // 15 5 19 9 20 23 15 18 11
    System.out.println(returnIndexNumbers(map, test2)); // 1 2 3 24 25 26

}

// return a string whit the index o letter from the alphabet from a map
public static String returnIndexNumbers(Map<Integer, String> map, String test) {
    // we create a stringBuilder for not creating every time a string
    StringBuilder result = new StringBuilder();
    test.toLowerCase();
    // we iterate the string
    for (int i = 0; i < test.length(); i++) {
        // save the letter into a string from a char
        String letter = String.valueOf(test.charAt(i));

        // iterate over map, and if it match save the index to string
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            if (letter.equals(entry.getValue())) {
                result.append(entry.getKey()+ " ");
            }
        }

    }
    // convert stringBuilder to a string. Wrapper
    return String.valueOf(result);
}


// for every character from the alphabet starting from 0
// we give them an index (0 - 25)
public static Map<Integer, String> alphabetFrom0Index(char[] alphabet) {
    // we defined a map that contains a pair of key-value
    // key is the index of the character & value is the character itself
    Map<Integer, String> map = new HashMap<>();

    for (int i = 0; i < alphabet.length; i++) {
        // i is the position of the character from the array
        // and i+1 to start from 1, and not from 0
        map.put(i+1, String.valueOf(alphabet[i]));
    }
    return map;
}

// print index and letter from a map
public static void printIndexAndLetters(Map<Integer, String> map) {
    map.entrySet().stream().forEach((Map.Entry<Integer, String> entry) -> {
        System.out.println(entry.getKey() + " " + entry.getValue());
    });
}

}

Eduard A
  • 370
  • 3
  • 9