Results 1 to 2 of 2
Thread: Variable errors
- 10-31-2010, 11:03 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Variable errors
Hey,
I cannot figure out why the compiler is telling me that it cannot find the variable "output" even though I declared it in a previous method and returned it.
The compiler says "cannot find symbol"
"variable output"
"class Cipher"
Java Code:import java.io.*; /** * @(#)Cipher.java * * * @author * @version 1.00 2010/10/21 */ public class Cipher { public static void main(String[] args) { header(); // Displays header for program userInterface(); //Prompts user to determine action, input encryption key, and manipulates the file } /** *Outputs the header of the program */ public static void header() { System.out.println("Welcome to Cipher"); System.out.println("Please follow the on screen instructions so the YOU can"); System.out.println("encrypt, decrypt, or all of the above to any .txt files"); } /** *Prompts the user for values and calls methods that encrypt and decrypts files * */ public static void userInterface() { Scanner console = new Scanner(System.in); String choice = ""; Scanner input = getInputScanner(console); //Asks user for input file name to be manipulated System.out.print("Would you like to (E)ncrypt or (D)ecrypt a message? Or (Q)uit? "); choice = console.next(); boolean exit = true; while (exit) { String key = getKey(console); if (choice.equalsIgnoreCase("e")) { encrypt(key, input, output); } else if (choice.equalsIgnoreCase("d")) { decrypt(key, input, output); } else if (choice.equalsIgnoreCase("q")) { System.exit(1); } else { System.out.print("Incorrect value. Please try again. "); exit = false; } } } /** *Calls the encrypts method and prints the encrypted text onto a .txt file * *@param key key used to shift the information *@param input input scanner that scans file *@param output output PrintStream that moves the text into the file */ public static void encrypt(String key, Scanner input, PrintStream output) { while (input.hasNextLine()) { String line = encryptLine(key, input.nextLine()); output.println(line); } } /** *Calls the decrypts method and prints the decrypts text onto a .txt file * *@param key key used to shift the information *@param input input scanner that scans file *@param output output PrintStream that moves the text into the file */ public static void decrypt(String key, Scanner input, PrintStream output) { while (input.hasNextLine()) { String line = decryptLine(key, input.nextLine()); output.println(line); } } /** *Prompts the user to input a key and then checks the validity of a string * *@param console console for user input *@return key */ public static String getKey(Scanner console) { boolean valid = false; String key = ""; while(!valid) { System.out.print("Key? "); key = console.next(); valid = true; for (int i = 0; i < key.length(); i++) { if (key.charAt(i) < 'A' || key.charAt(i) > 'Z') { valid = false; } } } return key; } /** *Encrypts the line by shifting the characters given in the key * *@param key key will determine the index of the shift *@param line line of code that the scanner reads *@return line as encrypted String */ public static String encryptLine(String key, String line) { Scanner scanline = new Scanner(line); int newline = 0; //String line = ""; while(scanline.hasNext()) { for (int i = 0; i < key.length(); i++){ if (scanline.hasNext()) { String str = scanline.next(); for (int j = 0; j < str.length(); j++) { if (str.charAt(j) + key.charAt(i) <= 'Z') { newline = (str.charAt(j) + (key.charAt(i) - 'A')); } else if (str.charAt(j) + key.charAt(i) > 'Z') { newline = 'A' + ('Z' % key.charAt(i)); } } } } } line = new Character((char)newline).toString(); return line; } public static String decryptLine(String key, String line) { } public static Scanner getInputScanner(Scanner console) { Scanner input = null; System.out.print("Input filename?"); String filename = console.next(); Boolean repeat = false; while (!repeat) { try { input = new Scanner(new File(filename)); repeat = true; } catch (FileNotFoundException e) { System.out.print("Please input a valid filename: "); } } } public static PrintStream getOutputPrintStream(Scanner console) { PrintStream output = null; String overwrite = null; System.out.print("Output filename?"); File outputfile = new File(console.next()); boolean exists = outputfile.exists(); try { if (exists) { System.out.print("Do you want to overwrite? (y,n) "); overwrite = console.next(); if (overwrite.equalsIgnoreCase("y")) { output = new PrintStream(outputfile); } else { System.out.print("Please input a new file name: "); } } } catch (FileNotFoundException e) { System.out.print("Please input a valid filename: "); } }Last edited by Fubarable; 11-01-2010 at 12:35 AM. Reason: Moderator edit: code tags added
-
Let's not start a new thread when there's already an active thread on this question here: Help please!
Thread locked.
Similar Threads
-
errors
By santosh chauhan in forum New To JavaReplies: 5Last Post: 07-26-2010, 07:59 PM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
Help with three errors -.-
By Insomniac Riot in forum New To JavaReplies: 5Last Post: 03-30-2010, 06:52 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Static variable context Errors ?
By Shyam Singh in forum New To JavaReplies: 16Last Post: 08-08-2008, 09:11 PM


LinkBack URL
About LinkBacks

Bookmarks