Reputation: 25
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?
Upvotes: 1
Views: 2939
Reputation: 17168
Those are compiler errors, and each one tells you exactly what is wrong. Here's a quick list of the issues and resolutions (without writing all your code for you):
cannot find symbol
: This means that you're using a name that's undefined. For instance, on line 24, you use stdIn.nextString()
, but Scanner
objects do not have a method called nextString
(you probably want either next()
or nextLine()
). Thus, the compiler "cannot find the symbol" nextString
because it doesn't exist (or charAT
, because of the capitalization error).variable X is already defined
: This means exactly what it sounds like. When you get this error, it's because you're declaring variables twice. Once you declare a variable (e.g. char lowercase
), you can simply use it (e.g. lowercase = 'a';
) within that scope without declaring it again.missing method body
: This means you've got a method stub declared. Unless they are abstract, all methods must have a body enclosed in curly braces, and must return the type they are declared to return. In your particular case, you just need to get rid of the semicolons at the end of your encrypt
and decrypt
declarations.return outside method
means that you have a return
statement that's floating in a class all by itself, which wouldn't make any sense. In this case, this problem will go away once you get rid of the semicolons at the end of encrypt
and decrypt
, so that those blocks will be recognized as method bodies.illegal start of type
is a more subtle error, but in your case it's just happening because of the fact that those blocks are currently at the class level rather than being method bodies. That problem will go away when you fix the return outside method
issue.Upvotes: 1