Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: else without if error

  1. #1
    allexif is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Question else without if error

    The complete code is:

    public class LetterCheck2 {
    public static void main (String args[]){

    char simbol = 'A';
    simbol=(char)(128.0*Math.random());

    if(simbol >= 'A' && simbol <= 'Z'){
    System.out.println("Simbolul reprezinta o litera mare si anume " +simbol);
    }
    else {
    if(simbol >= 'a' && simbol <= 'z'){
    System.out.println("Simbolul reprezinta o litera mica si anume "+simbol);
    }
    }
    else {
    System.out.println("Simbolul nu reprezinta nici o litera");
    }
    }
    }


    The error I receive is:

    LetterCheck.java:15 'else without if' else {

    Can you tell me where I'm wrong? Thank you in advance!

  2. #2
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    your are using two else in code convert one of the else to else-if
    Java Code:
    if{
    
    }
    else if{
    
    }
    
    // more else if staements if needed
    
    else{
    
    }
    Java Code:
    public class LetterCheck2 {
    public static void main (String args[]){
    
    char simbol = 'A';
    simbol=(char)(128.0*Math.random());
    
    if(simbol >= 'A' && simbol <= 'Z'){
    System.out.println("Simbolul reprezinta o litera mare si anume " +simbol);
    } 
    
    else if(simbol >= 'a' && simbol <= 'z'){
    System.out.println("Simbolul reprezinta o litera mica si anume "+simbol);
    }
    
    else {
    System.out.println("Simbolul nu reprezinta nici o litera");
    }
    }
    }
    Last edited by fakepics500; 07-16-2011 at 02:45 PM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,618
    Rep Power
    23

    Default

    Can you tell me where I'm wrong?
    If you properly format your code aligning the closing } with the opening { you would see if there are mismatched {}s

    @fakepics500 Why didn't you align your posted code?

    Java Code:
    public class LetterCheck2 {
       public static void main (String args[]) {
       
          char simbol = 'A';
          simbol=(char)(128.0*Math.random());
          
          if(simbol >= 'A' && simbol <= 'Z'){
             System.out.println("Simbolul reprezinta o litera mare si anume " +simbol);
    
          } else {
                if(simbol >= 'a' && simbol <= 'z'){
                   System.out.println("Simbolul reprezinta o litera mica si anume "+simbol);
                }
    
          }   else {           // What if does this else belong to???
              System.out.println("Simbolul nu reprezinta nici o litera");
          }
       } // end main()
    } // end class
    Last edited by Norm; 07-16-2011 at 04:10 PM.
    fakepics500 likes this.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 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
  •