import java.io.*;
import java.util.*;
public class GentCanadian
{
public static void main (String[] args) throws IOException
{
BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));
String lowerInput = ""; // declaring variable to work outside the loop
while (!lowerInput.equals("quit"))
{
System.out.print ("Please enter a sentence to translate (Enter 'quit' to leave)");
String input = objReader.readLine ();
lowerInput = input.toLowerCase(); // any form of "quit" will make the loop exit
Translate newSentence = new Translate(input);
String output = newSentence.getOutput();
System.out.println (output); // printing output
}
}
}
class Translate
{
// declaring variables
private String word = "";
private String newWord = "";
private String line = "";
private String output = "";
private char lastLetter;
private char secondLastLetter;
private int wordLength;
Translate(String l)
{
line = l;
Translation();
getOutput(); // accessor method
}
private void Translation()
{
Scanner freader = new Scanner(line);
StringBuffer newPhrase = new StringBuffer ("");
while (freader.hasNext())
{
String word = freader.next();
wordLength = word.length(); // gets the length of each word
if (wordLength >= 5)
{
lastLetter = word.charAt(wordLength - 1);
secondLastLetter = word.charAt(wordLength - 2);
if (secondLastLetter == 'o' && lastLetter == 'r') // last two letters must be "OR"
{
String word2 = word.substring(0, wordLength - 2); // extracts all letters but the "OR"
newWord = word2.concat("our"); // adds "OUR" to the end of the word
}
else
{
newWord = word; // keeps the same word
}
}
else
{
newWord = word;
}
newPhrase.append(newWord);
newPhrase.append(' '); // add a space
output = newPhrase.toString(); // convert back to a string
}
}
String getOutput()
{
return output; // returns the whole output
}
}
我在drJava上遇到错误,这是我的类应该使用的编译器,在该编译器中我没有收到任何其他Java编译器。在第17行,当我有一条语句lowerInput = input.toLowerCase();它给我一个错误,说我不能将值赋给lowerInput,因为它是不可变的,并且已经被赋值了。如果有人知道我该如何解决该错误,请响应,因为我需要找出答案。