[英]String into Array and conversion java
Okay so if I have a strings(can be anything) that I need to put into an array(letter by letter), then convert the letters of that array to numbers using this switch statement: 好吧,如果我有一个需要放入一个数组(一个字母一个字母)的字符串(可以是任何东西),然后使用以下switch语句将该数组的字母转换为数字:
public static char[] convert(char c)
{
switch(c)
{
case 'A' :
case 'B' :
case 'C' :
return new char[]{'2'};
case 'D' :
case 'E' :
case 'F' :
return new char[]{'3'};
case 'G' :
case 'H' :
case 'I' :
return new char[]{'4'};
case 'J' :
case 'K' :
case 'L' :
return new char[]{'5'};
case 'M' :
case 'N' :
case 'O' :
return new char[]{'6'};
case 'P' :
case 'R' :
case 'S' :
return new char[]{'7'};
case 'T' :
case 'U' :
case 'V' :
return new char[]{'8'};
default:
return new char[]{'9'};
}
}
finally after converting it I need to change the converted array back into a string. 最后,在将其转换后,我需要将转换后的数组改回字符串。
how would i approach without importing anything but scanner 除了扫描仪外,我如何导入而不导入其他任何内容
For example using the word = words.next(); 例如,使用单词= words.next(); (the string is BBCNEWS) (字符串是BBCNEWS)
//get the input from standard in or wherever it is
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String output = "";
String word = scanner.nextLine();
char[] wordChars = word.toCharArray();
for(char wordChar: wordChars){
output = output.concat(new String(convert(wordChar)));
}
//do something with the output
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.