Yeah, I figured out how to do everything. Yes I didn't have the ROT13 method correct, but I figured it out:
//Begin hideLetter method
public static char hideLetter(String message)
{
for (int i = 0; i < message.length(); i++)
{
char c = message.charAt(i);
if (c >= 'a' && c <= 'm') c += 13;
else if (c >= 'n' && c <= 'z') c -= 13;
else if (c >= 'A' && c <= 'M') c += 13;
else if (c >= 'A' && c <= 'Z') c -= 13;
else
c = message.charAt(i);
return c;
}
//This return value will never be used, but is here because the control doesn't know that.
return 'a';
}
}
That seemed to do the job different letters, and:
public static void main(String[] args) throws Exception
{
Scanner in = new Scanner(System.in);
//Asks user what to do
System.out.println("This program opens a .txt file with a message, converts it to a hidden message, and then saves the hidden message as a new .txt file.");
System.out.print("Enter the directory and filename of the message you wish you hide, without '.txt': ");
String filename = in.nextLine();
//Creates a file instance
java.io.File file = new java.io.File(filename + ".txt");
//Error if the file doesn't exist
if (file.exists() == false)
{
System.out.println("The file specified does not exist.");
System.exit(0);
}
//Creates a file
System.out.print("Enter the directory and filename that you want your hidden message stored in, without '.txt': ");
String newFileName = in.nextLine();
java.io.PrintWriter newFile = new java.io.PrintWriter(newFileName + ".txt");
java.io.PrintWriter output = new java.io.PrintWriter(newFile);
Scanner input = new Scanner(file);
//Sets message to the empty string
String message = "";
//Program uses a delimiter that would most likely not be in the message that is to be converted.
input.useDelimiter(" ");
//Reads data from the file
while (input.hasNext())
{
message = input.next();
}
input.close();
//Emtpy console messages are for readability
System.out.println("Here is your input file's text: ");
System.out.println();
System.out.println(message);
System.out.println();
System.out.println("Here is your hidden message:");
System.out.println();
//Calls the hideLetter method within the for loop. Loop will continuously output the hidden letters from the file.
for (int ind = 0; ind < message.length(); ind ++)
{
char newLetter = hideLetter(String.valueOf(message.charAt(ind)));
System.out.print(newLetter);
output.print(newLetter);
}
System.out.println();
output.close();
}
Should handle the input/output. Perhaps there was a more efficient way but the program was due by midnight yesterday and I was scrambling to get it done, since I left it to the last minute. Thank you for your reply though!