Question: I want some one fix this code. I want the user if enter abc and the shift number for example 3 the output should be

I want some one fix this code. I want the user if enter abc and the shift number for example 3 the output should be "cde" and if the user enter xyz the output will be "zab"

import java.util.*;

class Main {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

//collect values from the user input

System.out.println("Enter the 3 letter");

String input = reader.nextLine();

System.out.println("Enter the 3 letter");

int shift = reader.nextInt();

calculateShift(input,shift);

}

public static String calculateShift(String msg, int shift)

{

char[] alphabet = {'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'};

String s = "";

for(int x = 0; x < msg.length(); x++)

{

char currentLetter = msg.charAt(x) ;

char letterLocation= alphabet.indexOf(currentLetter);

if (letterLocation > 'z'){

letterLocation= alphabet.indexOf(currentLetter)- (26-shift);

s = s+letterLocation;

}

else{

letterLocation= alphabet.indexOf(currentLetter)+shift;

s = s+letterLocation;}

}

return s;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!