Java字母将arraylist更改为不带逗号但包含空格的字符串
试图完成coderbyte的挑战:“使用Java语言,让函数LetterChanges(str)获取要传递的str参数,并使用以下算法对其进行修改。用字母表中紧跟其后的字母替换字符串中的每个字母(即c变为d,z变为a)。然后将新字符串(a、e、i、o、u)中的每个元音大写,最后返回修改后的字符串。“ 我遇到的问题是替换是在字符之间拉空格,但我需要它来保留单词之间的空格。有更好的解决办法吗Java字母将arraylist更改为不带逗号但包含空格的字符串,java,arrays,arraylist,replace,Java,Arrays,Arraylist,Replace,试图完成coderbyte的挑战:“使用Java语言,让函数LetterChanges(str)获取要传递的str参数,并使用以下算法对其进行修改。用字母表中紧跟其后的字母替换字符串中的每个字母(即c变为d,z变为a)。然后将新字符串(a、e、i、o、u)中的每个元音大写,最后返回修改后的字符串。“ 我遇到的问题是替换是在字符之间拉空格,但我需要它来保留单词之间的空格。有更好的解决办法吗 import java.util.Arrays; import java.util.Scanner; pu
import java.util.Arrays;
import java.util.Scanner;
public class nextLetter {
public static String LetterChanges(String str) {
String[] inputString = str.replaceAll("[^a-zA-Z ]", "").split("");
String[] alph= "abcdefghijklmnopqrstuvwxyz".split("");
String[] vowel ="aeiouy".split("");
for(int i=0; i<inputString.length; i++){
int index= Arrays.asList(alph).indexOf(inputString[i])+1;
inputString[i]= alph[index];
if(Arrays.asList(vowel).indexOf(inputString[i])>0){
inputString[i]= inputString[i].toUpperCase();
}
}
//System.out.println(Arrays.toString(inputString));
return Arrays.toString(inputString)
.replace(" ","")
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "")//remove the left bracket
.replace(" ","")
.trim();
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter a sentence");
System.out.print(LetterChanges(s.nextLine()));
}
}
导入java.util.array;
导入java.util.Scanner;
公共类nextLetter{
公共静态字符串字母更改(字符串str){
String[]inputString=str.replaceAll(“[^a-zA-Z]”),split(“”);
字符串[]alph=“abcdefghijklmnopqrstuvxyz”。拆分(“”);
字符串[]元音=“aeiouy”.split(“”);
对于(int i=0;i0){
inputString[i]=inputString[i].toUpperCase();
}
}
//System.out.println(Arrays.toString(inputString));
返回Arrays.toString(inputString)
.替换(“,”)
.replace(“,”)//删除逗号
.replace(“[”,”“)//拆下右支架
.replace(“]”,“”)//卸下左支架
.替换(“,”)
.trim();
}
公共静态void main(字符串[]args){
扫描仪s=新的扫描仪(System.in);
System.out.println(“输入句子”);
系统输出打印(字体更改(s.nextLine());
}
}
我也不介意任何关于如何改进的建议 注意:我已将方法名称更改为更具描述性的名称。该方法假定您只使用小写字母
public static void main(String[] args){
System.out.println(shiftLetters("abcdz")); //bcdea
}
public static String shiftLetters(String str){
StringBuilder shiftedWord = new StringBuilder();
for (int i = 0; i < str.length(); i++){
char currentChar = str.charAt(i);
if (currentChar != ' '){
currentChar += 1;
if (currentChar > 'z'){
currentChar = 'a';
}
}
shiftedWord.append(currentChar);
}
return shiftedWord.toString();
}
注意:我已将方法名称更改为更具描述性的名称。该方法假定您只使用小写字母
public static void main(String[] args){
System.out.println(shiftLetters("abcdz")); //bcdea
}
public static String shiftLetters(String str){
StringBuilder shiftedWord = new StringBuilder();
for (int i = 0; i < str.length(); i++){
char currentChar = str.charAt(i);
if (currentChar != ' '){
currentChar += 1;
if (currentChar > 'z'){
currentChar = 'a';
}
}
shiftedWord.append(currentChar);
}
return shiftedWord.toString();
}
这将保留所有其他字符并处理元音
public static String LetterChanges(String str)
{
str = str.toLowerCase();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if ('a' <= c && c <= 'z')
{
c = (c == 'z') ? 'a' : (char) (c + 1);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
c = Character.toUpperCase(c);
}
}
sb.append(c);
}
return sb.toString();
}
公共静态字符串字母更改(字符串str)
{
str=str.toLowerCase();
StringBuilder sb=新的StringBuilder();
对于(int i=0;i if('a'这将保留所有其他字符并处理元音
public static String LetterChanges(String str)
{
str = str.toLowerCase();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if ('a' <= c && c <= 'z')
{
c = (c == 'z') ? 'a' : (char) (c + 1);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
c = Character.toUpperCase(c);
}
}
sb.append(c);
}
return sb.toString();
}
公共静态字符串字母更改(字符串str)
{
str=str.toLowerCase();
StringBuilder sb=新的StringBuilder();
对于(int i=0;i 如果('a'如果有固定的字母和交换算法,则可以使用静态字典
public static HashMap<String,String> dictionary = new HashMap<>();
static{
dictionary.put(" ", " ");
dictionary.put("a", "b");
dictionary.put("b", "c");
dictionary.put("c", "d");
dictionary.put("d", "E");
.
.
dictionary.put("z", "A");
}
public static String shiftLetters(String str){
StringBuffer response = new StringBuffer();
for (int i = 0; i < str.length(); i++){
response.append(dictionary.get(String.valueOf(str.charAt(i))));
}
return response.toString();
}
publicstatichashmap dictionary=newhashmap();
静止的{
字典。放(“,”);
放在字典里(“a”,“b”);
放在字典里(“b”、“c”);
放在字典里(“c”、“d”);
放在字典里(“d”、“E”);
.
.
放在字典里(“z”,“A”);
}
公共静态字符串移位器(字符串str){
StringBuffer响应=新的StringBuffer();
对于(int i=0;i
如果有固定的alphabeth和交换算法,您可以使用静态字典
public static HashMap<String,String> dictionary = new HashMap<>();
static{
dictionary.put(" ", " ");
dictionary.put("a", "b");
dictionary.put("b", "c");
dictionary.put("c", "d");
dictionary.put("d", "E");
.
.
dictionary.put("z", "A");
}
public static String shiftLetters(String str){
StringBuffer response = new StringBuffer();
for (int i = 0; i < str.length(); i++){
response.append(dictionary.get(String.valueOf(str.charAt(i))));
}
return response.toString();
}
publicstatichashmap dictionary=newhashmap();
静止的{
字典。放(“,”);
放在字典里(“a”,“b”);
放在字典里(“b”、“c”);
放在字典里(“c”、“d”);
放在字典里(“d”、“E”);
.
.
放在字典里(“z”,“A”);
}
公共静态字符串移位器(字符串str){
StringBuffer响应=新的StringBuffer();
对于(int i=0;i
那么,您的问题到底是什么?当我输入“测试句子”时,输出是UFTUATFOUFOF,而它仍然应该是两个独立的“单词”你正在删除所有的空白。如果你想保留它,就不要删除它。无论如何@Michael有一个更好的方法。通过代码的遍历更新了我的答案。那么,你到底有什么问题?当我输入“测试句子”时,输出是uftuatfouff,而它仍然应该是两个独立的“单词”你正在删除所有的空白。如果你想保留它,请不要删除它。无论如何,@Michael有一个更好的方法。通过对代码的浏览更新了我的答案