dimanche 30 avril 2017

Swap words in sentence with String Buffer

Im having trouble with an application I have to build for college.

Develop an app that accepts following sentence from a user “Hickory Dickory, Ben. The mouse ran up the clock”. The app should change to word Ben with the word Dock. The app should then return the sentence “Hickory Dickory, Dock. The mouse ran up the clock”.

Some tips we were given are: 1. if there is space 2. if there is a char in the string at index 1 with a D 3. if there is a char in the string at index 2 with a O 4. if there is a char in the string at index 3 with a C 5. if there is a char in the string at index 4 with a K 6. if there is a space

This what I have tried but it doesnt work:

public void compute(){
  for(int i=0;i<sentence.length();i++){
     if(sentence.charAt(i)==' ')
        strBuff.append(' ')&&
        sentence.charAt(i)=='B'
           strBuff.append('D')&&
           sentence.charAt(i)=='e'
              strBuff.append('O')&&
              sentence.charAt(i)=='n'
                 strBuff.append('C')&&
                 sentence.charAt(i)==' '
                    strBuff.append('K');

     }
     else{
        error="Sorry please try again";
     }

  }
  newWord=stringBuff.toString();
}

We are to use string buffers, if statements and loops to swap the words. I just need help with the compute part. Please help.

Aucun commentaire:

Enregistrer un commentaire