Results 1 to 14 of 14
Thread: Help please!
- 10-31-2010, 07:04 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
-
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!
-
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:
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".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 #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.
- 10-31-2010, 09:08 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
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.
- 11-01-2010, 12:11 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Ok, now I am having issues only with the "output" variable errors on line 46 and 50
-
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.
- 11-01-2010, 12:43 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
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: "); } }
- 11-01-2010, 02:08 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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
- 11-01-2010, 02:10 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
I dont understand the relevance?
- 11-01-2010, 02:18 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
relevance
.... <hijack post deleted>....
Last edited by Fubarable; 11-01-2010 at 02:31 AM. Reason: moderator edit: hijack post deleted
- 11-01-2010, 02:21 AM #11
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
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
-
I can't tell what's wrong based on the snippets provided. Can you post your whole class?
- 11-01-2010, 02:48 AM #13
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
Please help quick because its due in 20 min and Im desperateJava 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: "); } } }
-
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.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks