else if

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    else if

    I am stuck here and have done various research on else if statements and it seems to be what im meant to do. Please help if you can.


    Code:
        private boolean isValidGuess() 
        {
            if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)));
            {
                return true;
            }
            
                     
            
           else if (!Character.isLowerCase(guess))
                {
                    return false;
                    System.out.println(guess + " is not a lower case entry");
                }
                
                
                
                 
    
               
                
                 else if(guess == '*')
                {
                    System.out.println("You gave up!");
                }
                else
                {
                    return false
                    System.out.println("You guessed " + guess + " already");
                }
    
            
        }
    I am getting an else without if error.

    Also, as you might have seen, Im also stuck with does not contain, guessed already. Any input appreciated.

    Brendan
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Your final return statement is missing a semicolon...tha t's the only issue I saw with a brief look-over.

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      Thanks for that but still doesn't solve the else with out an if problem :(

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        The structure of your code looks like this:

        if{}
        else if {
        else if {}
        else {}
        }

        Do you see the problem now?

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          I'm sorry i don't.

          I have a method opening {

          i have an if stament with {}

          i have an else if statements {}

          close method }

          Sorry if im being stupid!

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Copy and paste the error message verbatim; you get an "else without if" error
            because you have a trailing semi colon at the end of your first if clause.

            You also get a bit of whining about a missing semi colon. If you fix that you get
            an error saying "unreachabl e code" because you want to print something after
            returning from the method.

            kind regards,

            Jos

            Comment

            • brendanmcdonagh
              New Member
              • Nov 2007
              • 153

              #7
              Hi Jos, thanks for your reply(again!)

              I'm using netbeans so included all errors and all code. Sorry to overwhelm you. just in case you need it.

              init:
              deps-jar:
              Compiling 1 source file to C:\Documents and Settings\Brenda n.LIVINGROOM\My Documents\M257_ 08J_TMA01_Downl oadBrendanMcDon agh\TMA01 Download\TMA01\ build\classes
              C:\Documents and Settings\Brenda n.LIVINGROOM\My Documents\M257_ 08J_TMA01_Downl oadBrendanMcDon agh\TMA01 Download\TMA01\ src\tma01q2\Gue sser.java:56: 'else' without 'if'
              else if (!Character.isL owerCase(guess) )


              C:\Documents and Settings\Brenda n.LIVINGROOM\My Documents\M257_ 08J_TMA01_Downl oadBrendanMcDon agh\TMA01 Download\TMA01\ src\tma01q2\Gue sser.java:95: illegal start of expression
              public String toString()
              C:\Documents and Settings\Brenda n.LIVINGROOM\My Documents\M257_ 08J_TMA01_Downl oadBrendanMcDon agh\TMA01 Download\TMA01\ src\tma01q2\Gue sser.java:113: ';' expected
              }


              C:\Documents and Settings\Brenda n.LIVINGROOM\My Documents\M257_ 08J_TMA01_Downl oadBrendanMcDon agh\TMA01 Download\TMA01\ src\tma01q2\Gue sser.java:118: '}' expected
              }


              Code:
              package tma01q2;
              
              import java.util.Scanner;
              import java.util.TreeSet;
              import tma01q1.Player;
              
              /**
               * Title: Guesser class  
               * Description: This class represents the player guessing a code word
               * and is to be completed according to instructions in TMA01 Question 2   
               */
              
              public class Guesser extends Player
              {
                  private char guess;
                  private Set<Character> guessedAlready;
                  final char EXIT_CHARACTER = '*';
                  
              
                  //complete the constructor    
                  public Guesser(String name)
                  {
                      super(name);
                      guessedAlready = new TreeSet<Character>();
                      
                 
                  }
                  
                  //complete the following methods
                  
                  //readChar returns the first character entered by a user
                  //after conversion to lowercase form
                  private char readChar()
                  {
                      Scanner aScanner = new Scanner(System.in);
                      String currentline = aScanner.nextLine();
                      currentLine.toLowerCase();
                      return currentLine.charAt(0);
                      
                  }    
                  
                  // isValidGuess returns true if the value of guess is valid
                  // (a previously unguessed, lowercase alphabet letter, or the exit character)
                  // otherwise it returns false.
                  // This method also displays an appropriate error message 
                  // if an invalid input has been detected.
                  private boolean isValidGuess() 
                  {
                      if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)));
                      {
                          return true;
                      }
                      
                               
                      
                     else if (!Character.isLowerCase(guess))
                          {
                              return false;
                              System.out.println(guess + " is not a lower case entry");
                          }
                          
                          
                          
                           
              
                         
                          
                           else if(guess == '*')
                          {
                              System.out.println("You gave up!");
                          }
                          else
                          {
                              return false;
                              System.out.println("You guessed " + guess + " already");
                          }
              
                      
                  }
                  
                  // inGuessedAlready returns true if the received character is
                  // stored in the guessedAlready structure, otherwise it returns false
                  private boolean inGuessedAlready()
                  {   
                      if(guessedAlready.contains(guess))
                      {
                          return true;
                      }
                      else
                      {
                          return false;
                      }
                  
                  // Returns the Guesser's name and a list of guessed characters
                  public String toString()
                  {
                      System.out.println(this.getName() + "guesses " + guessedAlready.toString());
                  }
                  
                  // This is the main method that a driver class uses.
                  // It returns a 'valid' character
                  // (EXIT_CHARACTER or lowercase and not previously guessed).
                  // The guess and guessedAlready variables are updated when required
                  public char turn()
                  {  
                      System.out.println("Please have a guess or enter the exit character");
                      guess = this.readChar();
                      while(!this.isValidGuess())
                      {
                         guess = this.readChar();
                      }
                      guessedAlready.add(guess);
                  }    
              
              
              
                  
              }

              Comment

              • brendanmcdonagh
                New Member
                • Nov 2007
                • 153

                #8
                I found the other errors, just a missing } but still have else without an if

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by brendanmcdonagh
                  I found the other errors, just a missing } but still have else without an if
                  Reread my reply again; you have a construct like this:

                  Code:
                  if ( ... ); // <--- note the trailing semicolon here
                  {
                     // this is always executed
                  }
                  kind regards,

                  Jos

                  Comment

                  • asedt
                    New Member
                    • Jun 2008
                    • 130

                    #10
                    Originally posted by brendanmcdonagh
                    I found the other errors, just a missing } but still have else without an if
                    I guess you have nestled the if/elseif/else statment worng, but you can post you current code agen.

                    And you indentation/whitespace hurt my eyes.

                    Comment

                    • chaarmann
                      Recognized Expert Contributor
                      • Nov 2007
                      • 785

                      #11
                      An "else" must always be after an "if", directly!. "always" means you cannot just write an "else" clause by itself! And "directly" means, you cannot write some other code between the end of the "if" and beginning of the "else".
                      So where is the "if" for the "else" in line 10? It's not the one in line 3!

                      What you have written is following:
                      if (condition) action; // in your case, action is not there, so it's empty! The action cannot follow AFTER the semicolon!
                      { // start of a code block
                      some code // this code is ALWAYS executed, independent of the condition!.
                      } // end of code block
                      else // where is my if-statement? there is only some stange code above me, but no "if"!

                      What you may not know are 2 cases that you combined:
                      1.)
                      Code:
                      x = 7;
                      y = false;
                      if (y = (x==7)); // this is a legal statement. notice "=" and "==". It sets y to true. Always. Now the action of the if-statement is called. But this action is not given, so it does nothing!
                      x = 9; // this is alway set, regardless of the previous "if" statement
                      2.)
                      Code:
                      x=7;
                      { // legal. it's just the start of a code block. You could write "synchronized" in front if you like. It has nothing to do with an "if" statement.
                        y=10;
                        int z=20;
                      }
                      y=30; // just set y to 30 that was just set to 10 before.
                      // x has still old value = 7
                      // but you cannot access z anymore!. Z was only valid inside the code block. If you try to write "z=10;", it will not compile. (Scope!)

                      Comment

                      • brendanmcdonagh
                        New Member
                        • Nov 2007
                        • 153

                        #12
                        Thjanks everyone,

                        Your suggestion worked Jos - as always - thanks

                        Regards

                        Brendan

                        Comment

                        • brendanmcdonagh
                          New Member
                          • Nov 2007
                          • 153

                          #13
                          Code:
                          private boolean isValidGuess() 
                              {
                                  if ((Character.isLowerCase(guess)) && (!guessedAlready.contains(guess)))
                                  {
                                      return true;
                                  }
                                  
                                           
                                  
                                 else if (!Character.isLowerCase(guess))
                                      {
                                          return false;
                                          System.out.println(guess + " is not a lower case entry");
                                      }
                                      
                                      
                                      
                                       
                          
                                     
                                      
                                       else if(guess == '*')
                                      {
                                          return false;
                                          System.out.println("You gave up!");
                                          
                                      }
                                      else
                                      {
                                          return false;
                                          System.out.println("You guessed " + guess + " already");
                                      }
                          
                                  
                              }
                          I'm really sorry about this but i ve 3 unreachable errors now to do with this method.

                          my understanding is this
                          I can have an if stament
                          {
                          it's execution code
                          }
                          then as many else if()
                          {
                          exectubale code
                          }

                          then an else for for all other options
                          else()
                          {
                          }

                          am i wrong?

                          Comment

                          • brendanmcdonagh
                            New Member
                            • Nov 2007
                            • 153

                            #14
                            sorry solved, the return statements has to be last in statement block,

                            sorry, sorry, sorry!

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by brendanmcdonagh
                              sorry solved, the return statements has to be last in statement block,

                              sorry, sorry, sorry!
                              Not necessarily. You just have to make sure that every possible path of execution has it.

                              Comment

                              Working...