簡體   English   中英

Java Letter將arraylist更改為不帶逗號但包含空格的字符串

[英]Java Letter changes arraylist to string without commas but including white spaces

嘗試從coderbyte處完成這一挑戰:“使用Java語言,讓函件LetterChanges(str)接受傳遞的str參數,並使用以下算法對其進行修改。將字符串中的每個字母替換為字母后面的字母(即c變為d,z變為a)。然后在此新字符串(a,e,i,o,u)中將每個元音大寫,最后返回修改后的字符串。

我遇到的問題是替換是在字符之間插入空白,但是我需要它來保留單詞之間的空白。 有更好的解決方案嗎?

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())); 
}
}

我也不會介意如何改善這一點!

注意:我已經將方法名稱更改為更具描述性的名稱。 該方法假定您只使用小寫字母。

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();
}

這是該程序的一般邏輯流程:創建一個累積的StringBuilder對象,該對象最終將成為方法的返回值。 遍歷字符串中的所有字符; 如果該字符是空白字符,則不必理會它,並將其原樣添加到StringBuilder上。 Else ,向當前字符添加一個。 請注意, char整數 (4.2.1)基本類型,因此您可以這樣將int添加到char中。 如果是特殊情況,新char超出了正常的az范圍,請將其設置回a

利用Java 8的API

public static String functionalShiftLetters(String str){
    return str
        .chars()
        .map(c -> c != ' ' ? c + 1 : c)
        .map(c -> c > 'z'? 'a' : c)
        .collect(StringBuilder::new,
                   StringBuilder::appendCodePoint, StringBuilder::append)
        .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();
}

輸入 :abcdefghijklmnopqrstuvwxyz 1234567890

輸出 :bcdEfghIjklmnOpqrstUvwxyzA 1234567890

如果具有固定的字母和交換算法,則可以使用靜態字典。

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();
    }   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM