/************************************************************* DaleHollowayProg3
ID: 3538027 • Letter: #
Question
/*************************************************************
DaleHollowayProg3.java
Written by Dale Holloway
6/20/13
This program is a substitution ciper using a shift value
and the ASCII value of user input alphabetic characters between A-Z
to return encrypted and unencrypted UPPERCASE return values.
********************************************************************/
import java.util.Scanner;
public class DaleHollowayProg3
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String plain;//plain text input from user
String encrypt="";//text after encryption
int shifted; //number of spaces to shift
int avalue; //ascii character value
String decrypt; //text after decryption
System.out.println("Please enter text to encrypt");//User enters text to encrypt
plain = stdIn.nextLine();
plain = plain.toUpperCase();
while(plain.indexOf(" ")>=0)
plain = plain.replace(" ", "");
System.out.println("Please enter shift value");//User enters shift value
shifted = stdIn.nextInt();
for(int i = 0;i<plain.length();i++)
{
avalue = plain.charAt(i);
if(plain.charAt(i)>='A' && plain.charAt(i)<='Z')
{
for(int j=0;j<Math.abs(shifted);j++)
{
if(shifted>0)
{
avalue--;
if(avalue<'A')avalue='Z';
}
if(shifted<0)
{
avalue++;
if(avalue>'Z')avalue='A';
}//above ensures characters are between A-Z
}//end inner
encrypt = encrypt + (char)avalue;
}//end outer
System.out.println(encrypt);
stdIn = new Scanner(System.in);
System.out.println("Please enter text to decrypt");
decrypt = stdIn.nextLine();
decrypt = decrypt.toUpperCase();
while(decrypt.indexOf(" ")>0)
decrypt = decrypt.replace(" ","");
System.out.println("Please enter shift value");
shifted = stdIn.nextInt();
plain = "";
for(int i3 = 0; i3 < decrypt.length();i3++)
{
for(int i2 = 0; i2 <Math.abs(shifted);i2++)
{
if(shifted > 0)
{
avalue--; if(avalue<'A')avalue = 'Z';
}
if(shifted < 0)
{
avalue++; if(avalue>'Z')avalue = 'A';
}
}//end inner
}//end outer
plain = plain + (char)avalue;
System.out.println(plain);
}//End Method
}
}//End Class
Explanation / Answer
please rate - thanks
that was a hard one, misplaced }
I only did the encrypt, not the decrypt, that prints 4 characters
/*************************************************************
DaleHollowayProg3.java
Written by Dale Holloway
6/20/13
This program is a substitution ciper using a shift value
and the ASCII value of user input alphabetic characters between A-Z
to return encrypted and unencrypted UPPERCASE return values.
********************************************************************/
import java.util.Scanner;
public class DaleHollowayProg3
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String plain;//plain text input from user
String encrypt="";//text after encryption
int shifted; //number of spaces to shift
int avalue=0; //ascii character value
String decrypt; //text after decryption
System.out.println("Please enter text to encrypt");//User enters text to encrypt
plain = stdIn.nextLine();
plain = plain.toUpperCase();
while(plain.indexOf(" ")>=0)
plain = plain.replace(" ", "");
System.out.println("Please enter shift value");//User enters shift value
shifted = stdIn.nextInt();
for(int i = 0;i<plain.length();i++)
{avalue = plain.charAt(i);
if(plain.charAt(i)>='A' && plain.charAt(i)<='Z')
{for(int j=0;j<Math.abs(shifted);j++)
{
if(shifted>0)
{avalue--;
if(avalue<'A')
avalue='Z';
}
if(shifted<0)
{avalue++;
if(avalue>'Z')
avalue='A';
}//above ensures characters are between A-Z
}
}//end inner
encrypt = encrypt + (char)avalue;
}//end outer
System.out.println(encrypt);
stdIn = new Scanner(System.in);
System.out.println("Please enter text to decrypt");
decrypt = stdIn.nextLine();
decrypt = decrypt.toUpperCase();
while(decrypt.indexOf(" ")>0)
decrypt = decrypt.replace(" ","");
System.out.println("Please enter shift value");
shifted = stdIn.nextInt();
plain = "";
for(int i3 = 0; i3 < decrypt.length();i3++)
{
for(int i2 = 0; i2 <Math.abs(shifted);i2++)
{
if(shifted > 0)
{
avalue--; if(avalue<'A')avalue = 'Z';
}
if(shifted < 0)
{
avalue++; if(avalue>'Z')avalue = 'A';
}
}//end inner
plain = plain + (char)avalue;
}//end outer
System.out.println(plain);
}//End Method
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.