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

Here is my another code I\'m working on now, but it has errors that I don\'t kno

ID: 3580486 • Letter: H

Question

Here is my another code I'm working on now, but it has errors that I don't know how to fix it. Can someone help me out? Give details of how you correct it or explain why? Thank you so much.

Required Member Methods:

encrypt - which will take in a single letter and return a result of type char.  
encryptAll - which will manipulate class variables in a loop to encrypt all of them.   
getMessage - which will read in and store the message string in a class variable.  
getCode - which will read in and store the encoding value.  
printOut - which will print out the current values of the class variables. To make it more readable, make sure that you skip spaces in the string (don't change them). For example, on our ASCII machines, 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 promopts 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

       

        public void encryptAll()

        {

            char[] messageArray = message.toCharArray();

            for (int n = 0; n < message.length(); n++)

            {

                        messageArray[n] = encrypt(message.toCharArray()); // here is the error I have

                System.out.println(messageArray[n]);

            }

        }

}

Explanation / Answer

Error : toCharArray()

It returns a newly allocated character array, whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

It is not required to our programme.

Solution : use function --- > charAt(index)

it returns character at given index postion.

Correct Programme Code :


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 promopts 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

public void encryptAll()
{
char[] messageArray = message.toCharArray();
for (int n = 0; n < message.length(); n++)
{
messageArray[n] = encrypt(message.charAt(n)); // here is the error I have
System.out.println(messageArray[n]);
}
}
}

Output :


C:UsersAshokDesktopCheggjava>javac Redcoats.java

C:UsersAshokDesktopCheggjava>java Redcoats
Enter a message to encrypt:
dsf
The message you entered was: dsf
Enter the code with which to encrypt the message:
4
The code you entered was: 4
h
w
j

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote