简体   繁体   中英

Insert a comma after each item but not the last item

I have a program in java that when you enter a sentence and the program tells you how many palindrome words there are and outputs the words. However, when I output the words I can't seem to get a comma after each output. For example if I input "Abba is running to the radar" It outputs that there's 2 palindromes and that the palindromes are " Abba radar ". I however want it to output the palindromes as "Abba, Radar". No matter how I do it I can either get "Abba Radar" or "Abba, Radar". Any help would be appreciated.

The code

package strings;

import javax.swing.*;

public class Palindrome2 {

public static void main(String[] args) {
    String word = JOptionPane.showInputDialog("Words that are the same forwards and backwards are called palindromes.\nThis program determines if the words are palindromes.\n\nEnter a sentence(do not include a punctuation mark):");

    String newWord[] = word.split(" ");
    String palindromeWords = "";
    int count = 0;

    for (int i = 0; i < newWord.length; i++) {
        String result = new StringBuffer(newWord[i]).reverse().toString();
        if (newWord[i].toLowerCase().equals(result.toLowerCase())) {
            count++;
            palindromeWords = palindromeWords + " " + newWord[i];
        }
    }
    JOptionPane.showMessageDialog(null, "There are " + count + " palindromes in this sentence");
    if (count != 0) {
        JOptionPane.showMessageDialog(null, "The palindromes are:\n" + palindromeWords);
    } else {
        JOptionPane.showMessageDialog(null, "There isn't any palindromes.");
    }
}
}

Just modify your code to:

for (int i = 0; i < newWord.length; i++) {
    String result = new StringBuffer(newWord[i]).reverse().toString();
    if (newWord[i].toLowerCase().equals(result.toLowerCase())) {
        count++;
        palindromeWords = palindromeWords + newWord[i] + ",";
    }
}

After the for loop, substring it to remove the last comma:

palindromeWords = palindromeWords.substring(0,palindromeWords.length()-1);

The code could also be

    if (newWord[i].toLowerCase().equals(result.toLowerCase())) {
        count++;
        if (count > 1)
        {
            palindromeWords += ", ";
        }
        palindromeWords += newWord[i];
    }

The comma goes before the new word after the first was found...

public static void main(String[] args) {
    String word = JOptionPane.showInputDialog("Words that are the same forwards and backwards are called palindromes.\nThis program determines if the words are palindromes.\n\nEnter a sentence(do not include a punctuation mark):");

    String newWord[] = word.split(" ");
    StringBuffer palindromeWords = new StringBuffer();
    int count = 0;

    for (int i = 0; i < newWord.length; i++) {
        String result = new StringBuffer(newWord[i]).reverse().toString();
         if (newWord[i].toLowerCase().equals(result.toLowerCase())) {
            if (count > 0) {
               palindromeWords.append(",");
            }
            palindromeWords.append(newWord[i]);
            count++;
        }
    }
JOptionPane.showMessageDialog(null, "There are " + count + " palindromes in this sentence");
    if (count != 0) {
        JOptionPane.showMessageDialog(null, "The palindromes are:\n" + palindromeWords.toString());
    } else {
        JOptionPane.showMessageDialog(null, "There isn't any palindromes.");
    }
}

So I altered palindromewords to be a StringBuffer and moved the count++ after the append operation. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM