试图将一个句子分成单词,然后交换每个单词的第一个和最后一个字母

时间:2013-12-13 22:26:51

标签: java

有谁知道为什么它没有成功?

以下是主要类的代码:

public class FirstNLast {
    private String word[];
    private String sentence = "";
    private String newWord;
    private StringBuilder strBuff;
    private int len;
    private char firstLetter;
    private char lastLetter;

    public FirstNLast(){
        word = sentence.split(" ");
        newWord = "";
        strBuff = new StringBuilder();
        len = 0;
        firstLetter = ' ';
        lastLetter = ' ';
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }

    public void compute(){
        len = word.length;
        firstLetter=word[0].charAt(0);
        lastLetter=word[len-1].charAt(len-1);

        for (int i=0; i < word.length; i = i + 1) {
            if (word[i].charAt(i)==firstLetter) {
                strBuff.append(lastLetter);
            }
            else if (word[i].charAt(i)==lastLetter) {
                strBuff.append(firstLetter);
            }
            else {           
                strBuff.append(word[i].charAt(i));
            }
            newWord = strBuff.toString();
        }
    }

    public String getSentence(){
        return sentence;
    }
}

以下是我的App类的代码:

import javax.swing.JOptionPane;

public class FirstNLastApp {

    public static void main(String[]args) {
        String sentence = "";
        String[] word = sentence.split(" ");

        String newWord;
        StringBuilder strBuff;
        int len=0;
        char firstLetter;
        char lastLetter;

        FirstNLast fnl = new FirstNLast();

        sentence = JOptionPane.showInputDialog(null, "Please enter a sentence");

        fnl.setSentence(sentence);

        fnl.compute();

        sentence=fnl.getSentence();

        JOptionPane.showMessageDialog(null, "The new word is " + sentence);
    }
}

3 个答案:

答案 0 :(得分:2)

我没有看你的代码,但我会在标题中回答你的问题:

    String[] words = sentence.split(" ");
    String newSentence = "";

    for(String word : words){
        char[] letters = word.trim().toCharArray();
        char firstChar = letters[0];
        letters[0] = letters[letters.length - 1];
        letters[letters.length - 1] = firstChar;
        newSentence += new String(letters) + " ";
    }

    return newSentence;

要了解您的代码无效的原因,您应该按照 Stephen C 的提示进行操作:)

答案 1 :(得分:1)

我的建议是使用IDE的内置调试器。设置一些断点,并使用单步执行和调试器的变量查看器来查看正在发生的事情。

我无法向您提供有关如何执行此操作的详细说明,因为它取决于您的IDE。但毫无疑问,有关如何执行此操作的帮助/教程信息。在那之后,这是一个学习如何为自己做这件事的问题。 (你越早学会为自己做,越好!)

答案 2 :(得分:0)

这是我的Instantiable类的代码。它适用于交换单词的第一个和最后一个字母。但后来我的问题是我需要输入一个句子并将其应用于句子中的每个单词,所以我单独离开这个类并创建了一个名为FirstNLast的新类,在那里我研究了如何分割句子。

public class FirstNLastWord {

    private String word;
    private String newWord;
    private StringBuffer strBuff;
    private int len;
    private char firstLetter;
    private char lastLetter;

    public FirstNLastWord() {

        word = "";
        newWord = "";
        len = 0;
        firstLetter = ' ';
        lastLetter = ' ';
    }
    public void setWord(String word) {
        this.word = word;
    }

    public void compute() {

        strBuff = new StringBuffer();
        len = word.length();
        firstLetter=word.charAt(0);
        lastLetter=word.charAt(len-1);

        for(int i=0; i < word.length(); i = i + 1) {

            if(word.charAt(i)==firstLetter) {
                strBuff.append(lastLetter);
            }
            else if(word.charAt(i)==lastLetter) {
                strBuff.append(firstLetter);
            }

            else {
                strBuff.append(word.charAt(i));

            }
            newWord = strBuff.toString();
        }
    }
    public String getNewWord() {
        return newWord;
    }
}

这是FirstNLast类,用于找出如何拆分句子。

public class FirstNLast{

    private String word[];
    private String sentence, newWord;
    private String newSentence;
    private StringBuffer strBuff;
          private int len;
    private char firstLetter;
    private char lastLetter;
    private FirstNLastWord fnl;

    public FirstNLast(){
        sentence = "";
        newSentence = "";
        strBuff = new StringBuffer();
        len = 0;
        firstLetter = ' ';
        lastLetter = ' ';
        fnl=new FirstNLastWord();
    }
    public void setSentence(String sentence){
        this.sentence = sentence;
    }
    public void compute(){

        word = sentence.split(" ");
        len = word.length;


        for(int i=0; i < word.length; i = i + 1){
            fnl.setWord(word[i]);
            fnl.compute();
            newSentence= newSentence+" "+fnl.getNewWord();
        }

     }

     public String getNewSentence(){
         return newSentence;
     }
}

最后我的app类输入了句子。它工作得很好。所以在所有3个班级。一个类可以更改单词的第一个和最后一个字母。一个类以相同的方式更改句子中的所有单词,第三个类输入并显示该句子。

import javax.swing.JOptionPane;

public class FirstNLastApp{

    public static void main(String[]args){


        String sentence="";
        String word[]=sentence.split(" ");
        String newSentence="";
        StringBuffer strBuff;
        int len=0;
        char firstLetter;
        char lastLetter;

        FirstNLast fnl = new FirstNLast();


        sentence = JOptionPane.showInputDialog(null, "Please enter a sentence");


        fnl.setSentence(sentence);


        fnl.compute();

        newSentence=fnl.getNewSentence();


        JOptionPane.showMessageDialog(null, "The new sentence is " + newSentence);

    }
}