Results 1 to 3 of 3
  1. #1
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default 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:

    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.");
              
          }
          
        }
        
      } 
    }
    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 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] + " ");
          
        }
        
      }
    }
    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:

    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?

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Else-Without-If and Other Issues That Don't Make Sense

    Java Code:
    else if(cipherDecipher.equalsIgnoreCase("n"));
    You've done the common trick of putting a semi-colon at the end there.

    Remove that and that should compile (barring other errors).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ssrock64 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Else-Without-If and Other Issues That Don't Make Sense

    Thanks! Usually I catch that the moment I type it, but I was in a bit of a rush today because I realized it's Monday, not Sunday and that the program is now officially late.

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2012, 04:31 PM
  2. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  3. Compilation error I can't make any sense of
    By qoncept in forum New To Java
    Replies: 5
    Last Post: 02-02-2011, 03:41 PM
  4. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM

Posting Permissions

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