Results 1 to 3 of 3
- 11-12-2012, 05:04 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Else-Without-If and Other Issues That Don't Make Sense
Hello, everyone. I'm writing a program to generate a cipher alphabet and to allow the user to create a ciphered message or attempt to decipher another, and the compiler keeps throwing up an Else-Without-If error where I find none no matter how much I look. Here's the main class, in which the error traces to line 78:
And here's the unfinished class it references (both referenced classes are identical at this point):Java Code:import java.io.*; import java.util.*; public class CipherTester { static final String[] baseAlphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; static Scanner in = new Scanner(System.in); static String cipherDecipher; public static void main(String[] args) { System.out.println("Do you wish to create a cipher message (y/n)?"); cipherDecipher = in.nextLine(); if(cipherDecipher.equalsIgnoreCase("y")) { Cipher cipher = new Cipher(baseAlphabet); cipher.createCipherAlphabet(); } else if(cipherDecipher.equalsIgnoreCase("n")); { System.out.println("Do you wish to decipher a message (y/n)?"); cipherDecipher = in.nextLine(); if(cipherDecipher.equalsIgnoreCase("y")) { Decipher decipher = new Decipher(baseAlphabet); decipher.createCipherAlphabet(); } else if(cipherDecipher.equalsIgnoreCase("n")) { System.out.println("You have chosen not to perform any action."); } else { System.out.println("You did not enter a valid option. Re-start program and try again?"); cipherDecipher = in.nextLine(); if(cipherDecipher.equalsIgnoreCase("y")) { main(args); } else { System.out.println("Done."); } } } else { System.out.println("You did not enter a valid option. Re-start program and try again?"); cipherDecipher = in.nextLine(); if(cipherDecipher.equalsIgnoreCase("y")) { main(args); } else { System.out.println("Done."); } } } }
I include the second class, too, because when I remove the error-causing else from the main class and run it to test the rest of the program, the output for creating a cipher alphabet acts strangely, creating and displaying the alphabet as intended, but then running the decipher prompt, too. To be clearer, here's exactly what it displays:Java Code:import java.io.*; import java.util.*; public class Cipher { String[] myBaseAlphabet; String[] myCipherAlphabet; public Cipher(String[] baseAlphabet) { myBaseAlphabet = baseAlphabet; myCipherAlphabet = new String[myBaseAlphabet.length]; } public void createCipherAlphabet() { Random rn = new Random(); for(int i = 0; i < myBaseAlphabet.length; i++) { myCipherAlphabet[i] = myBaseAlphabet[rn.nextInt(myBaseAlphabet.length)]; System.out.print(myCipherAlphabet[i] + " "); } } }
Do you wish to create a cipher message (y/n)?
[DrJava Input Box, answered "y"]
s v b q k y e h n l z w w x y t b s l l v e d x t v Do you wish to decipher a message (y/n)?
[DrJava Input Box]
I feel that this odd behavior is possibly related.
Any ideas?
- 11-12-2012, 05:38 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Else-Without-If and Other Issues That Don't Make Sense
You've done the common trick of putting a semi-colon at the end there.Java Code:else if(cipherDecipher.equalsIgnoreCase("n"));
Remove that and that should compile (barring other errors).Please do not ask for code as refusal often offends.
- 11-12-2012, 05:46 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
I need to make chess pieces visible and select two squares to make any pieces to move
By Sufiyana in forum New To JavaReplies: 2Last Post: 10-31-2012, 04:31 PM -
can i make a program to make keyboard and mouse idle or not responding for 10 second
By 3ammary in forum Advanced JavaReplies: 4Last Post: 07-23-2011, 08:08 PM -
Compilation error I can't make any sense of
By qoncept in forum New To JavaReplies: 5Last Post: 02-02-2011, 03:41 PM -
jdk issues
By artemff in forum New To JavaReplies: 3Last Post: 01-02-2010, 03:18 AM -
Issues with Jva I.O
By Annatar01 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks