I'm trying to assign a number to each letter in a sentence depending on what letter it is.
However, when trying to set up the array using the String length it breaks idk why.
import java.util.Scanner;
public class cypher {
static Scanner obj = new Scanner(System.in);
static String initial_sent;
static char char_single;
public static void main(String[] args) {
System.out.println("Instert sentence to be converted:");
initial_sent = obj.next();
initial_sent.toLowerCase();
String[] char_array = new String[initial_sent.length()];
for(int i = 0; i < initial_sent.length(); i++){
char_single = initial_sent.charAt(i);
switch(char_single){
case 'a':
char_array[i] = "1";
break;
case 'b':
char_array[i] = "2";
break;
case 'c':
char_array[i] = "3";
break;
case 'd':
char_array[i] = "4";
break;
case 'e':
char_array[i] = "5";
break;
case 'f':
char_array[i] = "6";
break;
case 'g':
char_array[i] = "7";
break;
case 'h':
char_array[i] = "8";
break;
case 'i':
char_array[i] = "9";
break;
case 'j':
char_array[i] = "10";
break;
case 'k':
char_array[i] = "11";
break;
case 'l':
char_array[i] = "12";
break;
case 'm':
char_array[i] = "13";
break;
case 'n':
char_array[i] = "14";
break;
case 'o':
char_array[i] = "15";
break;
case 'p':
char_array[i] = "16";
break;
case 'q':
char_array[i] = "17";
break;
case 'r':
char_array[i] = "18";
break;
case 's':
char_array[i] = "19";
break;
case 't':
char_array[i] = "20";
break;
case 'u':
char_array[i] = "21";
break;
case 'v':
char_array[i] = "22";
break;
case 'w':
char_array[i] = "23";
break;
case 'x':
char_array[i] = "24";
break;
case 'y':
char_array[i] = "25";
break;
case 'z':
char_array[i] = "26";
break;
default:
char_array[i] = "0";
break;
}
}
System.out.println(char_array);
obj.close();
}
}
For example when sending the line "aabb", I want it to return "1122", but instead it send this gibberish "[Ljava.lang.String;@3b9a45b3".
Thanks in advance.
First store the output of .toLowerCase()
to a String variable to process it further. To print the Array char_array
, you should combine its members and store it in a string like I have done here. The members of char_array
are combined in String result
. Now you can print the result
.
import java.util.Scanner;
public class cypher {
static Scanner obj = new Scanner(System.in);
static String initial_sent;
static char char_single;
public static void main(String[] args) {
System.out.println("Instert sentence to be converted:");
initial_sent = obj.next();
initial_sent = initial_sent.toLowerCase();
String[] char_array = new String[initial_sent.length()];
for (int i = 0; i < initial_sent.length(); i++) {
char_single = initial_sent.charAt(i);
switch (char_single) {
case 'a':
char_array[i] = "1";
break;
case 'b':
char_array[i] = "2";
break;
case 'c':
char_array[i] = "3";
break;
case 'd':
char_array[i] = "4";
break;
case 'e':
char_array[i] = "5";
break;
case 'f':
char_array[i] = "6";
break;
case 'g':
char_array[i] = "7";
break;
case 'h':
char_array[i] = "8";
break;
case 'i':
char_array[i] = "9";
break;
case 'j':
char_array[i] = "10";
break;
case 'k':
char_array[i] = "11";
break;
case 'l':
char_array[i] = "12";
break;
case 'm':
char_array[i] = "13";
break;
case 'n':
char_array[i] = "14";
break;
case 'o':
char_array[i] = "15";
break;
case 'p':
char_array[i] = "16";
break;
case 'q':
char_array[i] = "17";
break;
case 'r':
char_array[i] = "18";
break;
case 's':
char_array[i] = "19";
break;
case 't':
char_array[i] = "20";
break;
case 'u':
char_array[i] = "21";
break;
case 'v':
char_array[i] = "22";
break;
case 'w':
char_array[i] = "23";
break;
case 'x':
char_array[i] = "24";
break;
case 'y':
char_array[i] = "25";
break;
case 'z':
char_array[i] = "26";
break;
default:
char_array[i] = "0";
break;
}
};
String result = "";
for (String key: char_array) {
result = result + key;
};
System.out.println(result);
}
}
If you want a separate the numbers in the result, you can edit the for loop and add ", "
to separate the numbers like-
String result = "";
for (String key: char_array) {
result = result + key+ ", ";
};