I'm relatively new to java (and programming in general), and I'm trying to improve. There's this project I'm trying to work on (not school related) that calls for the creation of a Caesar Cipher based on a string they entered, the direction they wish to shift, and by the number of positions in the alphabet they wish to shift by. AN example is below:
NORMAL ALPHABET: A B C D E F G H
SHIFT RIGHT BY 2: Y Z A B C D E F
SHIFT LEFT BY 3: D E F G H I J K
The thing is though, it is simple to do it if it's just a single character, but I've got no idea on how to proceed without using an array, which I'm not keen on. Any advice?
Update
I've managed to compile some code (through hours and hours of help) into the following below, however I'm encountering two errors with my main. Here is my methods file:
import java.util.Scanner;
public class HW4Methods
{
public static String readString(Scanner kb)
{
Scanner kbTwo = new Scanner(System.in);
System.out.print("Please Enter A String: ");
String userString = kbTwo.nextLine();
while(userString.equals(null) || (userString.isEmpty()))
{
System.out.print("Field Cannot Be Empty. Please Re-Enter: ");
userString = kbTwo.nextLine();
}
return userString;
}
public static int readAmountToShift(Scanner kb)
{
int amountToShift;
Scanner kbThree = new Scanner(System.in);
System.out.print("Please Enter the Amount You Wish To Shift: ");
amountToShift = kb.nextInt();
while((amountToShift < 0) || (amountToShift > 2000000000))
{
System.out.print("Number Not Within Specified Parameters. Please Re-Enter: ");
amountToShift = kb.nextInt();
}
return amountToShift;
}
public static String readDirection(Scanner kb)
{
Scanner kbFour = new Scanner(System.in);
System.out.print("Enter the Direction You Wish to Shift (Left or Right): ");
String shiftD = kb.next();
while(!shiftD.equalsIgnoreCase("left") && (!shiftD.equalsIgnoreCase("right")))
{
System.out.print("Invalid Direction. Enter the Direction You Wish to Shift (Left or Right): ");
shiftD = kbFour.next();
}
return shiftD;
}
public static int menu(Scanner kb)
{
int userChoice;
Scanner kbFive = new Scanner(System.in);
System.out.println("Please Choose From the Following:");
System.out.println("1. Enter New String.");
System.out.println("2. Encrypt String.");
System.out.println("3. Decrypt String.");
System.out.println("4. Quit.");
System.out.println("Please Enter Your Choice: ");
userChoice = kbFive.nextInt();
while((userChoice < 1)||(userChoice > 4))
{
System.out.print("Invalid Menu Choice. Please Try Again.");
userChoice = kbFive.nextInt();
}
return userChoice;
}
public static String encryptString(String origString, int amount, String direction)
{
origString = "";
for(int i=0;i < origString.length();i++)
{
char a = origString.charAt(i);
if(direction.equalsIgnoreCase("right"))
{
if(a + amount > 122)
{
origString += 97 +(a + amount - 123);
}
else if(a + amount > 90)
{
origString += 65+(a + amount - 91);
}
else if(a + amount > 97 && a + amount <= 122)
{
origString += (char)(a + amount);
}
else if(a + amount > 65 && a + amount <= 90)
{
origString += (char)(a + amount);
}
}
if(direction.equalsIgnoreCase("left"))
{
if(a+ amount < 65)
{
origString += (char)(90);
}
else if(a - amount < 91)
{
origString += 113 +(a - amount + 91);
}
else if(a + amount >= 97 && a + amount <= 122)
{
origString += (char)(a - amount);
}
else if(a + amount <= 65 && a + amount >= 91)
{
origString += (char)(a - amount);
}
}
}
return origString;
}
public static String decryptString(String encryptedString, int amount, String direction)
{
String decrypt = "";
for(int i=0;i < encryptedString.length();i++)
{
char a = encryptedString.charAt(i);
if(direction.equals("right"))
{
if(a - amount < 97)
{
decrypt += (char)(122) - (a - amount - 123);
}
else if(a - amount < 65)
{
decrypt += 90 - ((char)(a + amount) - 91);
}
else if(a - amount >= 97 && a - amount <= 122)
{
decrypt += (char)(a - amount);
}
else if(a - amount >= 65 && a - amount <= 90)
{
decrypt += (char)(a - amount);
}
}
if(direction.equals("left"))
{
if(a + amount > 90)
{
decrypt += 65 +((char)(a - amount) - 64);
}
else if(a + amount > 122)
{
decrypt += 97 + ((char)(a + amount) - 97);
}
else if(a + amount >= 65 && a + amount <= 91)
{
decrypt += (char)(a + amount);
}
else if(a + amount >= 97 && a + amount <= 122)
{
decrypt += (char)(a + amount);
}
}
}
return decrypt;
}
public static void display(String stringOne, String stringTwo)
{
System.out.println(stringOne + stringTwo);
}
}
And here is my main (in a different folder):
import java.util.Scanner;
public class CSCD210HW4
{
public static void main(String [] args)
{
int choice, amount;
Scanner kb = new Scanner(System.in);
String direction, origString, encryptedString, decryptedString;
origString = HW4Methods.readString(kb);
amount = HW4Methods.readAmountToShift(kb);
direction = HW4Methods.readDirection(kb);
encryptedString = HW4Methods.encryptString(origString, amount, direction);
HW4Methods.display("Your encrypted string is ", encryptedString);
do
{
choice = HW4Methods.menu(kb);
switch(choice)
{
case 1: origString = HW4Methods.readString(kb);
break;
case 2: amount = HW4Methods.readAmountToShift(kb);
direction = HW4Methods.readDirection(kb);
encryptedString = HW4Methods.encryptString(origString, amount, direction);
HW4Methods.display("Your encrypted string is ", encryptedString);
break;
case 3: direction = HW4Methods.readDirection(encryptedString);
amount = HW4Methods.readAmountToShift(encryptedString);
decryptedString = HW4Methods.decryptString(encryptedString, amount, direction);
HW4Methods.display("Your decrypted string is ", decryptedString);
break;
case 4: System.out.println("Thanks for playing");
}// end switch
}while(choice != 4);
}// end main
}// end class
I am encountering two errors in main, listed below:
CSCD210HW4.java:33: error: incompatible types: String cannot be converted to Scanner
amount = HW4Methods.readAmountToShift(encryptedString);
^
CSCD210HW4.java:32: error: incompatible types: String cannot be converted to Scanner
case 3: direction = HW4Methods.readDirection(encryptedString);
^
I don't know why I'm getting these errors, and it's driving me crazy.