簡體   English   中英

如何在輸出結束時消除空間?

[英]How to get rid of space at the end of output?

我的代碼檢查是否有一定數量的用戶輸入的字符串有重復的字符。 例如,如果我輸入字符串“ google”,“ paper”和“ water”,則代碼返回“ paper”和“ water”; 因為“ google”有兩個Os。

我將代碼部分放下,但是在打印時,輸出的最后一個字符串之后會出現一個空格,我無法弄清楚如何擺脫它。

import java.util.Scanner;
import java.util.*;

class words{

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number or words: ");
        String[] words = new String[sc.nextInt()];
        System.out.print("Enter the strings: ");
        boolean truth = false;

        for (int i = 0; i < words.length; i++) {
            words[i] = sc.next();
        }
        for(int i=0;i<words.length;i++){
            int j;
            for(j=1;j<words[i].length();j++) {
                if(words[i].charAt(j) == words[i].charAt(j-1)){
                    break;
                }
            }
            if(j==words[i].length()){
                truth = true;
                System.out.print(words[i]+" ");
            }
        }
        if(!truth){
            System.out.println("NONE");
        }
    }
}

功能使邏輯可讀

移動邏輯以檢查是否有重復的字符進入函數; 我將利用String.toCharArray()和較短的數組語法。 喜歡,

private static boolean repeatedChars(String s) {
    if (s == null) {
        return false;
    }
    char[] chars = s.toCharArray();
    for (int i = 0; i < chars.length - 1; i++) {
        if (chars[i] == chars[i + 1]) {
            return true;
        }
    }
    return false;
}

然后,您可以使用lambda根據沒有重復字符的words過濾words ,並使用Collectors.joining(CharSequence)進行Collectors.joining(CharSequence)

Scanner sc = new Scanner(System.in);
System.out.print("Enter the number or words: ");
String[] words = new String[sc.nextInt()];
System.out.print("Enter the strings: ");

for (int i = 0; i < words.length; i++) {
    words[i] = sc.next();
}
System.out.println(Arrays.stream(words).filter(s -> !repeatedChars(s))
        .collect(Collectors.joining(" ")));

並且 ,如果您需要顯示NONE消息,則可以重復使用Predicate<String>

Predicate<String> pred = s -> !repeatedChars(s);
if (Arrays.stream(words).anyMatch(pred)) {
    System.out.println(Arrays.stream(words).filter(pred).collect(Collectors.joining(" ")));
} else {
    System.out.println("NONE");
}

有一個簡單的解決方法來解決您的問題。 如果沒有連續重復,則不要立即打印每個單詞,而是將其添加到末尾帶有空格的String變量中,以便每個單詞都由空格分隔。 運行完循環后,檢查標志是否為假,如果為假,則打印NONE。 但是,如果為true,則打印結果字符串,在其中添加所有內容的末尾帶有.trim()的字符串。

for (int i = 0; i < words.length; i++) {
    words[i] = sc.next();
}
String result = ""; /*This is the string that holds all the strings that you need to print.*/

for(int i=0;i<words.length;i++){
    int j;
    for(j=1;j<words[i].length();j++) {
        if(words[i].charAt(j) == words[i].charAt(j-1)){
            break;
        }
    }
    if(j==words[i].length()){
        truth = true;
        result  = result + (words[i]+" ");
    }
}
if(!truth){
    System.out.println("NONE");
}
else{
    System.out.println(result.trim()); /*The trim function removes any redundant space in the beginning and the end of the string.*/
}

當然,這樣做會浪費大量的堆內存,但我想這是一個小型學習項目。 但是,請務必研究StringBuilder的用法,以避免在堆中創建大量內存!

暫無
暫無

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

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