简体   繁体   中英

Trying to get a sentence to be split into words then swap the first and last letter of each word

Does anyone know why it's not working out?

Here is the code for the main class:

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

Here is the code for my App class:

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

I didn't watch your code, but I will answer your question in the title:

    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;

To understand why your code is not working, you should follow Stephen C 's tips :)

My recommendation is to use your IDE's built-in debugger. Set some breakpoints, and use single stepping and the debugger's variable viewer to watch what is happening.

I can't give you detailed instructions on how to do this because it depends on your IDE. But there will no doubt be help / tutorial information on how to do this. After that, it is a matter of learning how to do it for yourself. (And the sooner you learn to do it for yourself, the better!)

Here is my code for the Instantiable class. It works for swapping the first and last letters of a word. But then my problem was I needed to input a sentence and apply this to each word in the sentence so I left this class alone and created a new class called FirstNLast where I worked out how to split a sentence.

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

Here is the FirstNLast class to figure out how to split the sentence.

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

And finally my app class to enter the sentence. It works perfectly fine. So in all 3 classes. One class to change the first and last letters of a word. One class to change all the words in a sentence in the same way and a third to enter and display the sentence.

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

    }
}

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