Results 1 to 14 of 14

Thread: Help please!

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

    Default Help please!

    Hey I'm relatively new to java and I have a project due tonight and I cannot figure out whats wrong with my code. The code is attached below and it is saved as a txt document because I cannot upload .java files.
    Attached Files Attached Files

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

    Default

    Just paste the code in the forum in your post, but be sure to surround it with code tags:

    Place this tag above your code block: [code]

    Place this tag below your code block: [/code]

    Note that they are different.

    Also, please explain in as much detail as necessary just what is wrong with your current program. The more you can tell us, often the better we can help you. Luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Since you're new here, I'll post the code in your attached file:

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class Cipher {
    
       public static void main(String[] args) {
          header();
          userInterface();
       }
    
       public static void header() {
       }
    
       public static void userInterface() {
    
          Scanner console = new Scanner(System.in);
          String choice = "";
    
          Scanner input = getInputScanner(console);
    
          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) {
    
             if (choice.equalsIgnoreCase("e")) {
                String key = getKey(console);
                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;
             }
          }
       }
    
       public static void encrypt(String key, Scanner input, PrintStream output) {
          while (input.hasNextLine()) {
             String encryptedtext = encryptLine(key, input.nextLine());
             output.println(encryptedline);
          }
       }
    
       public static void decrypt(String key, Scanner input, PrintStream output) {
          while (input.hasNextLine()) {
             String decryptedtext = decryptLine(key, input.nextLine());
             output.println(decryptedtext);
          }
       }
    
       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;
    
       }
    
       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' % 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;
          System.out.print("Output filename?");
          String outputfile = console.next();
          boolean exists = outputfile.exists();
    
          try {
             if (exists) {
                System.out.print("Do you want to overwrite? (y,n) ");
                String overwrite = console.next();
                if (overwrite.equalsIgnoreCase("y")) {
                   output = new PrintStream(new File(outputfile));
                }
    
                else {
                   System.out.print("Please input a new file name: ");
                }
             }
    
          } catch (FileNotFoundException e) {
             System.out.print("Please input a valid filename: ");
          }
          return output;
    
       }
    
    }

    I see that you have a lot of compiler errors in your code and it suggests that you are breaking a cardinal rule of coding: you are adding code to a broken program.

    Solution: don't do this. Only add small amounts of code at a time, compile the code frequently, and if the code doesn't compile, don't add any more code until the compile error(s) has/have been fixed. Otherwise you'll end up with a rat's nest of errors.

    Right now, I'd recommend that you try to fix each compiler error one at a time. If one confuses you, post the error here and ask about it.

    For instance here:
    Java Code:
             if (choice.equalsIgnoreCase("e")) {
                String key = getKey(console);
                encrypt(key, input, output); // Error #1
             }
    
             else if (choice.equalsIgnoreCase("d")) {
                decrypt(key, input, output); // Error #2
             }
    At Error #1 the compiler complains that output can't be resolved, and this error message makes sense as you've never declared nor initialized any variable in your program called "output".

    At Error #2, the compiler again complains about output not being resolvable, and again for the same reason, and also complains about key not being resolvable, and while yes, you declare key in the if block above, because it is declared inside the if block, it's scope is limited to the block it was declared in. In other words, outside of that if block, the key variable doesn't exist. If you need it elsewhere in the program, then you'll want to declare it in an outer block or as a class field.

    Much luck!
    Last edited by Fubarable; 10-31-2010 at 07:20 PM.

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

    Default Thanks for the help so far...

    Yeah,

    I wrote the majority of this code without ever compiling or running. I got it down from 45 errors to the eight I have now and the program is not complete yet.

    How would I declare the key variable outside of the if loop without losing the parameters that the if statement has put on them... also, I declared the "output" variable as a PrintStream.

    The empty methods represent code that I have yet to input.

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

    Default

    Ok, now I am having issues only with the "output" variable errors on line 46 and 50

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You probably have an issue of scope. Please show which line(s) is causing the error and show where you are declaring the output variable so that it is in the same scope as the line with the error.

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

    Default

    ok, I have tried moving the variable into the scope of the entire method with a return but it did not work either.

    Java Code:
    public static void encrypt(String key, Scanner input, PrintStream output) {
    	while (input.hasNextLine()) {
    		String line = encryptLine(key, input.nextLine());
       		output.println(line);
             }
    }
    
    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: ");
       			}
    
    		
       		}

  8. #8
    Prettypython is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Wanna know the source of my error

    ....<hijack post deleted> ....
    Last edited by Fubarable; 11-01-2010 at 02:31 AM. Reason: moderator edit: hijack post deleted

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

    Default

    I dont understand the relevance?

  10. #10
    Prettypython is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default relevance

    .... <hijack post deleted>....
    Last edited by Fubarable; 11-01-2010 at 02:31 AM. Reason: moderator edit: hijack post deleted

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

    Default

    yeah but this thread is only for the problems that I am having for my code. If you want to post a problem elsewhere, please create a new thread and entitle however you want.

    Some advice I recieved earlier involves copy, cut, and pasting your code into the forum using a code block.

    Java Code:
    //your code here

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

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

    Default

    Java Code:
    
    import java.util.*;
    import java.io.*;
    
    /**
     * @(#)Cipher.java
     *
     *
     * @author Michael Bazik
     * @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.print(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;
       			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: ");
       			}
    
    
       		}
    
    
    }
    Please help quick because its due in 20 min and Im desperate

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Problems:
    in getInputScanner, you need to get the input from within the while block. If you don't, if they don't enter a proper file name, it will repeat forever.

    In the same method, you need to return something.

    Same comments for getOutputPrintStream method.

    You need to call the getOutputPrintStream method to retrieve the PrintStream object that will be output variable for your encrypt and decrypt methods.

Posting Permissions

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