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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started