0

The below code takes 3 words on 3 different lines. The first word should be changed like all vowels should be replaced by %. The second word should be changed like all consonants should be replaced by #. The third word should be changed like all char should be converted to upper case. Then concatenate the three words and print them. For eg 1st, 2nd, 3rd` words are respectively:

how, are, you

Then, the result should be:

h%wa#eYOU

However, I get the result as:

[C@4769b07b[C@cc34f4dYOU

My code is:

Scanner sc = new Scanner(System.in);
String fword = sc.nextLine();
String sword = sc.nextLine();
String tword = sc.nextLine().toUpperCase();
char[] arr1 = fword.toCharArray();
char[] arr2 = sword.toCharArray();
for (int i = 0; i < arr1.length; i++) {
    if (arr1[i] == 'a' || arr1[i] == 'e' || arr1[i] == 'i' || arr1[i] == 'o'
            || arr1[i] == 'u' || arr1[i] == 'A' || arr1[i] == 'E'
            || arr1[i] == 'I' || arr1[i] == 'O' || arr1[i] == 'U')
        arr1[i] = '%';
}
for (int i = 0; i < arr2.length; i++) {
    if (arr2[i] != 'a' && arr2[i] != 'e' && arr2[i] != 'i' && arr2[i] != 'o'
            && arr2[i] != 'u' && arr2[i] != 'A' && arr2[i] != 'E'
            && arr2[i] != 'I' && arr2[i] != 'O' && arr2[i] != 'U')
        arr2[i] = '#';
}
String res1 = arr1.toString();
String res2 = arr2.toString();
System.out.println(res1 + res2 + tword);
Shreyash
  • 81
  • 1
  • 10
  • 1
    arrays don't override toString, so you get *Array Type* `(C) + @ + hashcode` *in hex*. - http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/lang/Object.java#l236 – aran Mar 04 '21 at 11:17
  • No need to deal with char arrays, just use a regex: `final String fword = sc.nextLine().replaceAll("(?i)[aeiou]", "%");` – oliver_t Mar 04 '21 at 11:27

0 Answers0