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

I am getting an error on drJava which is the compiler my class is supposed to use in which I do not recieve on any other compiler for java. At line 17 when I have the statement lowerInput = input.toLowerCase(); it gives me an error saying that I cannot assign a value to lowerInput because it is immutable and has already been given a value. If anyone knows how I can fix this error please respond as I need to figure this out.

  • This shouldn't be a compiler error but it might be some style checker that's complaining because you're reassigning `lowerInput` which could result in subtle bugs. If such a style checker exists there's most probably some way to either disable those checks globally or tell it to ignore `lowerInput` because "I know what I'm doing". – Thomas Dec 06 '18 at 15:20
  • okay, so I changed my file name from GentCanadian.dj to GentCanadian.java and now it gives me no errors, just warnings but the program now runs – Joshua Gentile Dec 06 '18 at 15:23

0 Answers0