我正在寫一個從程序中刪除一個字符的java程序。我的代碼是;從句子中刪除一個字符的Java程序
public class Ex1Program {
public void start() {
String sentence = getSentenceFromUser();
int randomPosition = getRandomPosition(sentence);
printCharacterToBeRemoved(sentence, randomPosition);
String changedSentence = removeCharacter(sentence, randomPosition);
printNewSentence(changedSentence);
}
private String getSentenceFromUser() {
System.out.print("Enter a sentence :");
String sentence = Keyboard.readInput();
return sentence;
}
int sentenceLength = sentence.length();
int randomPosition = (int)(Math.random() * sentenceLength) + 0;
return randomPosition;
}
private void printCharacterToBeRemoved(String sentence, int randomPosition) {
System.out.print("Removing " + sentence.charAt(randomPosition) + " from position" + randomPosition);
}
private String removeCharacter(String sentence, int randomPosition) {
String changedSentence = sentence.deleteCharAt(randomPosition);
return changedSentence;
}
private void printNewSentence(String changedSentence) {
System.out.print("New sentence is " + changedSentence);
}
}
我收到一個錯誤消息,當我編譯說:
Error: cannot find symbol symbol: method deleteCharAt(int) location: variable sentence of type java.lang.String
我花了數小時試圖解決這個問題,所以幫助將非常感激。
請問String有這樣一個方法,'deleteCharAt(int)'?檢查API:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html如果它不在API中,則它不存在。 – Patashu