Firstly, here are the instructions for the program I am to put together...
Provide a main method. It should: Get input for a string and a shift value Convert to upper case Only perform the following items on alphabetic characters between A and Z Utilize a for loop which uses postfix incrementing operator o Convert character to its ASCII equivalent (type cast) o Shift b y shift value entered above If you reach end of alphabe t, wrap around Example: A shifted to the left 2 would become Y o Convert back to its character equivalent (type cast) o Output the new character Get input for a string and a shift value Perform same steps above to convert the encrypted text back to plain text Be sure to get input again as a different encrypted text may be entered Main method can call separate method to perform encryption/decryption but not required. Utilize postfix increment/decrement operations and compound assignment operators for all math. E xample: x++ or x+=2.
The code I have so far is:
/*
JursekGregChapter12t.java
Greg Jursek
This program will encrypt entered text by a user input
shift value and then decrypt text in the same manner.
*/
import java.lang.*;
import java.util.*;
public class JursekGregChapter12t
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String encryptText; // text to be encrypted
String decryptText; // text to be decrypted
int shiftValue; // the number of spaces that will be shifted via user input
// User enters plain text for encryption
System.out.print("Please enter text to encrypt");
encryptText = stdIn.nextString();
// User enters a shift value
System.out.println("Please enter shift value");
shiftValue = stdIn.nextInt();
// System prints the encrypted text
String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
System.out.println(encryptedText);
// User enters text for decryption
System.out.print("Please enter text to decrypt");
decryptText = stdIn.nextString();
// User enters a shift value
System.out.println("Please enter shift value");
shiftValue = stdIn.nextInt();
// System prints the decrypted text
String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
System.out.println(decryptedText);
} // end main
// Shift and Character Manipulation
public static String shift(String enteredText, int shiftValue)
{
String convertedText = "";
for(int i = 0; i< enteredText.length(); i++)
{
char lowerCase = enteredText.charAT(i);
//Convert to upper case letters
char lowerCase = Character.toUpperCase(lowerCase);
int charNumber = upperLetter;
//Shift letters and wrap text
int rotateLetters = (charNumber + shiftValue) % 26;
char shiftLetters = (char) rotateShift;
//Populate new string of characters
convertedText += shiftLetters;
}
return convertedText;
}
// Encryption code
public static String encrypt(String enteredText, int shiftValue);
{
String encryptedString = rotate(enteredText , shiftValue);
return encryptedString;
}
// Decryption code
public static String decrypt(String enteredText, int shiftValue);
{
int negativeShiftValue = (-1) * (shiftValue);
String decryptedString = rotate(enteredText , negativeShiftValue);
return decryptedString;
}
} //end class JursekGregChapter12t
At this point I am getting a lot of strange errors and I'm not quite sure what's gone wrong...those errors are:
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:24: error: cannot find symbol
encryptText = stdIn.nextString();
^
symbol: method nextString()
location: variable stdIn of type Scanner
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:36: error: cannot find symbol
decryptText = stdIn.nextString();
^
symbol: method nextString()
location: variable stdIn of type Scanner
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:55: error: cannot find symbol
char lowerCase = enteredText.charAT(i);
^
symbol: method charAT(int)
location: variable enteredText of type String
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:58: error: variable lowerCase is already defined in method shift(String,int)
char lowerCase = Character.toUpperCase(lowerCase);
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:59: error: cannot find symbol
int charNumber = upperLetter;
^
symbol: variable upperLetter
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:63: error: cannot find symbol
char shiftLetters = (char) rotateShift;
^
symbol: variable rotateShift
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:72: error: missing method body, or declare abstract
public static String encrypt(String enteredText, int shiftValue);
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:74: error: cannot find symbol
String encryptedString = rotate(enteredText , shiftValue);
^
symbol: variable enteredText
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:74: error: cannot find symbol
String encryptedString = rotate(enteredText , shiftValue);
^
symbol: variable shiftValue
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:75: error: return outside method
return encryptedString;
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:79: error: missing method body, or declare abstract
public static String decrypt(String enteredText, int shiftValue);
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:81: error: cannot find symbol
int negativeShiftValue = (-1) * (shiftValue);
^
symbol: variable shiftValue
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:81: error: illegal start of type
int negativeShiftValue = (-1) * (shiftValue);
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:82: error: cannot find symbol
String decryptedString = rotate(enteredText , negativeShiftValue);
^
symbol: variable enteredText
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:83: error: return outside method
return decryptedString;
^
15 errors
[Finished in 0.7s with exit code 1]
Any help is appreciated.
Thanks.
After resolving some of the errors:
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:56: error: cannot find symbol
char lowerCase = enteredText.charAT(i);
^
symbol: method charAT(int)
location: variable enteredText of type String
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:59: error: variable lowerCase is already defined in method shift(String,int)
char lowerCase = Character.toUpperCase(lowerCase);
^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:60: error: cannot find symbol
int charNumber = upperLetter;
^
symbol: variable upperLetter
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:64: error: cannot find symbol
char shiftLetters = (char) rotateShift;
^
symbol: variable rotateShift
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:75: error: cannot find symbol
String encryptedString = rotate(enteredText , shiftValue);
^
symbol: method rotate(String,int)
location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:83: error: cannot find symbol
String decryptedString = rotate(enteredText , negativeShiftValue);
^
symbol: method rotate(String,int)
location: class JursekGregChapter12t
6 errors
[Finished in 0.7s with exit code 1]
I've spent the last 2 hours reading up on stuff and I still cant understand what's holding my code back here. I'm very much a novice programmer, Java is my first foray into it and I'm just completely stuck. Does anyone have any ideas?
char lowerCase = Character.toUpperCase(lowerCase);always gives me the creeps.