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

Java Code: I have an error in the code. import java.util.Scanner; import java.ut

ID: 655431 • Letter: J

Question

Java Code:

I have an error in the code.

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;


       public class java2 {
           public static final int ALPHASIZE = 26;
           public static final char[] Lalpha =
                 { 'a','b','c','d','e','f','g','h','i','j','k','l',
                 'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
                 };
           public static final char[] Ualpha =
                 {'A','B','C','D','E','F','G','H','I','J','K','L',
                 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                 };
           protected static char[] encryptU = new char[ALPHASIZE];
           protected static int[] decrypt = new int[ALPHASIZE];
           protected static char[] encryptL = new char[ALPHASIZE];

           Random rgenerator = new Random();

           public java2(){
                 int randNum = rgenerator.nextInt(ALPHASIZE);

               

                 for(int i=0; i                  {
               
                     do {
                         randNum = rgenerator.nextInt(26);

                      } while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));     

                     encryptU[i] = Ualpha[randNum];
                     encryptL[i] = Lalpha[randNum];
                     decrypt[i] = randNum;

                 }
           }

           public String encrypt(String secret)
           {
                 System.out.println(Arrays.toString(encryptU));
                     int position = 0;
                 char[] mess = secret.toCharArray();
                 for(int i = 0 ; i                  {
                     if(Character.isUpperCase(mess[i]))
                     {
                 for(int j = 0; j < encryptU.length; j++) {
                     if(mess[i]==Ualpha[j]) {
                         position = j;
                 }
                 mess[i] = encryptU[position];

                     }

                     }

                     if(Character.isLowerCase(mess[i]))
                     {
                 for(int j = 0; j < encryptU.length; j++) {
                     if(mess[i]==Lalpha[j]) {
                         position = j;
                 }
                 mess[i] = encryptL[position];

                     }

                     }

                 }
                 return new String(mess);
           }

      

public String decrypt(String secret)
{
char[] mess = secret.toCharArray();
for(int i = 0 ; i<mess.length;i++)
{
if(Character.isUpperCase(mess[i]))
{
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptU[j]){
mess[i] = Ualpha[j];

}
}
}   

if(Character.isLowerCase(mess[i]))
{   
for(int j = 0; j<ALPHASIZE; j++){
if(mess[i]==encryptL[j]){
mess[i] = Lalpha[j];

}
}
}
}

return new String(mess);
}
}

Explanation / Answer

Hello,

Your question is not complete. What are you trying to accomplish from this program ? However, when i tried to run it in my console, it worked fine once i added the parameter values for i.

import java.util.Scanner;

import java.util.Random;

import java.util.Arrays;

?

public class java2 {

public static final int ALPHASIZE = 26;

public static final char[] Lalpha =

{ 'a','b','c','d','e','f','g','h','i','j','k','l',

'm','n','o','p','q','r','s','t','u','v','w','x','y','z'

};

public static final char[] Ualpha =

{'A','B','C','D','E','F','G','H','I','J','K','L',

'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',

};

protected static char[] encryptU = new char[ALPHASIZE];

protected static int[] decrypt = new int[ALPHASIZE];

protected static char[] encryptL = new char[ALPHASIZE];

Random rgenerator = new Random();

public java2(){

int randNum = rgenerator.nextInt(ALPHASIZE);

for(int i=0; i<26; i++) {

do {

randNum = rgenerator.nextInt(26);

} while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));

encryptU[i] = Ualpha[randNum];

encryptL[i] = Lalpha[randNum];

decrypt[i] = randNum;

}

}

public String encrypt(String secret)

{

System.out.println(Arrays.toString(encryptU));

int position = 0;

char[] mess = secret.toCharArray();

for(int i = 0 ; i<26;i++) {

if(Character.isUpperCase(mess[i]))

{

for(int j = 0; j < encryptU.length; j++) {

if(mess[i]==Ualpha[j]) {

position = j;

}

mess[i] = encryptU[position];

}

}

if(Character.isLowerCase(mess[i]))

{

for(int j = 0; j < encryptU.length; j++) {

if(mess[i]==Lalpha[j]) {

position = j;

}

mess[i] = encryptL[position];

}

}

}

return new String(mess);

}

public String decrypt(String secret)

{

char[] mess = secret.toCharArray();

for(int i = 0 ; i<mess.length;i++)

{

if(Character.isUpperCase(mess[i]))

{

for(int j = 0; j<ALPHASIZE; j++){

if(mess[i]==encryptU[j]){

mess[i] = Ualpha[j];

}

}

}

if(Character.isLowerCase(mess[i]))

{

for(int j = 0; j<ALPHASIZE; j++){

if(mess[i]==encryptL[j]){

mess[i] = Lalpha[j];

}

}

}

}

return new String(mess);

}

}

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