Results 1 to 2 of 2

Thread: Variable errors

  1. #1
    hellzxmaker is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default 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 01:35 AM. Reason: Moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. errors
    By santosh chauhan in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 08:59 PM
  2. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  3. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 07:52 PM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. Static variable context Errors ?
    By Shyam Singh in forum New To Java
    Replies: 16
    Last Post: 08-08-2008, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •