Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help with me fix my code? I know there is something wrong with it, b

ID: 3582740 • Letter: C

Question

Can someone help with me fix my code? I know there is something wrong with it, but I don't know what exactly is the problem? Below is my code. Thank you so much.

For example, the message:

THE REDCOATS ARE COMING!

and the integer 5 would produce:

YMJ WJIHTFYX FWJ HTRNSL&

=================================================================

public class Redcoats
{

   public static void main(String[] args)
   {
       Messages message = new Messages();
      
       message.getMessage();
       message.getCode();
       message.encryptAll();
   }

}

import java.util.Scanner;

public class Messages
{
   private String message;
   private int code;
   Scanner keyboard = new Scanner(System.in);
  
   //This method prompts the user to enter a string and stores it in a string variable
   public void getMessage()
   {
       System.out.println("Enter a message to encrypt: ");
       message = keyboard.nextLine();
       System.out.println("The message you entered was: " + message);
       }// end getMessge
  
   //This method prompts the user to enter the code with which to encrypt the message
   public void getCode()
   {
       System.out.println("Enter the code with which to encrypt the message: ");
       code = keyboard.nextInt();
       System.out.println("The code you entered was: " + code);
       }//end getCode
  
   //This method passes in a single character and changes its value if it is not a space
   public char encrypt(char letter)
   {
       if (letter == ' ')
           return letter;
       else
           letter = (char)((int)letter + code);

       return letter;
       }//end encrypt
  
   // This method will manipulate class variables in a loop to encrypt all of them
   public void encryptAll()
   {
       char[] messageArray = message.toCharArray();
       for (int n = 0; n < message.length(); n++)
       {
           messageArray[n] = encrypt(message.charAt(n));
           System.out.println(messageArray[n]);
           }
          
       }
}

Explanation / Answer

Hi,

I am executing the above code but i could not trace any thing wrong in the code.

In class Messages the method encryptAll() you need to do a small change instead of using system.out.println()

use print() method.

see the bleow snap shot what i exceute