So im working on a project that will encrypt code based upon the number that the user submits. (WORKS). The issue is that the program have to exit if the user import 'q' ON the first option... (Where the program asks the user for an int value)
import java.util.Scanner;
public class Encryption {
/**
*
* Description: This class prompts the user for key and message, performs the encryption, and prints the encrypted message
* out to the screen.
* Date: 10/4/2012
*/
public Encryption()
{
//Empty Constructor
}
public void run()
{
//Allow the user to input.
Scanner scan = new Scanner(System.in);
//Variables
String str = "";
int count,count1 = 0;
char code;
//While loop to run code
while(true)
{
System.out.println("\nPlease enter the encryption key you would like to use The key (should be a number between 1 and 26): ");
count = scan.nextInt(); //Error here when importing q. How can i input q without generating an error
//Need it so it can buffer the Int
scan.nextLine();
//Ask users to input a code,
System.out.println("\nPlease enter a code: ");
str = scan.nextLine();
for(int x = 0; x < str.length(); x++){
//Convert the string into Char, so i can encrypt the code.
code = str.charAt(x);
//If the code is uppercase then run the forumala, so the after Z it will go to A.
if(Character.isUpperCase(code))
{
count1 = (char)('A' +(code - 'A' + count) % 26);
}
//Won't shift any symbols
else if(code != ' ' && code != ',' && code != '.' && code != ':' && code != '\'' && code != '\"' && code != '!'){
//After the letter 'z' the code would go to 'a'
count1 = (char)('a' +(code - 'a' + count) % 26);
}
else if (code == ',')
count1 = ',';
else if (code == '.')
count1 = '.';
else if (code == ':')
count1 = ':';
else if (code == '\'' )
count1 = '\'';
else if (code == '\"' )
count1 = '\"';
else if (code == '!' )
count1 = '!';
else
count1 = ' ';
//Print the encypted code
System.out.print((char)count1);
}
}
}
}

New Topic/Question
Reply



MultiQuote





|