Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: 'ELSE' missing 'IF'

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 'ELSE' missing 'IF'

    Hi there,

    This is my first post in the forums. Honestly I need your help.
    I'm a beginner at programming java. Currently studying systems engineering of course.
    I'll be happy to help you out when I become a little more knowledgable.

    Problem:
    I am having this issue displayed in the cmd:

    Asterisco.java:42: error: 'else' without 'if'
    else if ( c <= 1 )
    ^
    Asterisco.java:64: error: while expected
    }
    ^
    Asterisco.java:68: error: reached end of file while parsing
    3 errors


    Brief Overview of Program:

    The program prints a given number of * within a line, and then continues printing this line a given number of times.
    The number of * printed together in a line are the colums or columnas in spanish.
    The number of lines of * printed are the rows or lineas in spanish.

    An example should look like this in the cmd;

    Columns (Columnas): 4
    Rows (Lineas): 7

    ****
    ****
    ****
    ****
    ****
    ****
    ****

    Code of program:

    import java.util.Scanner;
    public class Asterisco{
        public static void main(String[] args ){
     
        Scanner sc = new Scanner(System.in);
        int c;
        int l;
        char r;
        int contern;
     
        String Aster = "*";
        contern = 2;
     
     
            do{
    //Openning message
     	        System.out.println( " \n\n    Fecha de Creacion: 30 de Enero del 2013\n    Autor: Andrew J. Redican " );	
    	        System.out.println( " \n\n    Bienvenido al Asterisco. \n    Por favor, ingrese los datos requeridos. \n \n " );
    // Requesting values	
    	        System.out.println( "Numero de columnas: ");
                c = sc.nextInt();
    	        System.out.println( "Numero de lineas: ");
    	        l = sc.nextInt();
    // If any values are 0 then do nothing.  
    			if ( c > 0 && l > 0 )
    			{
     
    				if ( c > 1 )
    				{  
     
    					while ( contern <= c )
    					{
    			            Aster = Aster + Aster;
    						contern++;
    					}
     
    					for ( int k=1; k <= l; k++)
    					{
    			            System.out.println( Aster );	        	    
    			        }
     
    				else if ( c <= 1 )
    				{
     
    				    for ( int k=1; k <= l; k++)
    					{
    			            System.out.println( Aster );
    				    }
     
    				}
     
    				Aster = "*";
     
            	}
     
                System.out.println( "\n Desea ejectuar el programa otra vez? S para Si, N para No: \n " );
     
    		    r = sc.next().charAt(0);
     
            } while (r == 'S' || r == 's' );
    //Closing message		
    		System.out.println( " \n\n    Gracias por usar Asterisco.java :)\n\n\n\n\n\n" );	
     
    	}
    }

    Another Doubt:

    Am I able to add Strings?

    For example:

    String word;
    word = "wa";

    word = word + word;


    If I can't do that I'll have to think another way of add a given number of times "*" next to each other and then print it in my program.


    I hope you can give me a hand.
    Andrew R.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 'ELSE' missing 'IF'

    Go through, and for each opening curly brace, find its closing curly brace. Then do the opposite. You have a mismatch in there.

    As for adding Strings, what happened when you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: 'ELSE' missing 'IF'

    Quote Originally Posted by Andrew Red View Post
    ... issue...

    Asterisco.java:42: error: 'else' without 'if'
    else if ( c <= 1 )
    ^
    That's not an "issue," it is an error. Inconsistent use of spaces and tabs makes the appearance (to us mere humans) to be different than what the compiler sees. (In Java, the compile ignores indentation and uses {} braces to delineate blocks.)


    When I reformatted your code so that the visual alignment is consistent with the control structures, here's what I see. (I added a comment to show the problem line:
    import java.util.Scanner;
    public class Asterisco{
        public static void main(String[] args ){
     
        Scanner sc = new Scanner(System.in);
        int c;
        int l;
        char r;
        int contern;
     
        String Aster = "*";
        contern = 2;
     
     
            do{
    //Openning message
                System.out.println( " \n\n    Fecha de Creacion: 30 de Enero del 2013\n    Autor: Andrew J. Redican " );    
                System.out.println( " \n\n    Bienvenido al Asterisco. \n    Por favor, ingrese los datos requeridos. \n \n " );
    // Requesting values    
                System.out.println( "Numero de columnas: ");
                c = sc.nextInt();
                System.out.println( "Numero de lineas: ");
                l = sc.nextInt();
    // If any values are 0 then do nothing.  
                if ( c > 0 && l > 0 )
                {
     
                    if ( c > 1 )
                    {  
     
                        while ( contern <= c )
                        {
                            Aster = Aster + Aster;
                            contern++;
                        }
     
                        for ( int k=1; k <= l; k++)
                        {
                            System.out.println( Aster );                    
                        }
     
                    else if ( c <= 1 ) // This is line number 42
                    {
     
                        for ( int k=1; k <= l; k++)
                        {
                            System.out.println( Aster );
                        }
    .
    .
    .

    Now does the error message make sense?


    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 'ELSE' missing 'IF'

    @ KevinWorkman

    Don't know since I haven't sorted out the first problem.

    By the way, I looked into it closely, 3 times, earch curly brace has its opposite pair.

    I think problem is because I am placing more than two instructions within the else if?
    Andrew R.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 'ELSE' missing 'IF'

    Sounds like I missed something REALLY basic in my java course.

    I removed the { thats extra on the code. Still displaying an error;

    Asterisco.java:42: error: 'else' without 'if'
    else if ( c <= 1 )
    ^
    1 error
    Andrew R.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 'ELSE' missing 'IF'

    Quote Originally Posted by Andrew Red View Post
    By the way, I looked into it closely, 3 times, earch curly brace has its opposite pair.
    Count again. You have 9 opening curly brackets, and only 8 closing brackets. One of them is missing its pair.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Location
    Honduras
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 'ELSE' missing 'IF'

    @ KevinWorkman

    You were right about an extra { yes.


    @ Zaphod_b

    Thanks man!

    I had to go over my book again.

    Great help. I'll be placing the code for any bypassers that might have the same error/issue.

    .
    .
    .
     
    			if ( c > 0 && l > 0 )
    			{
     
    				if ( c > 1 )
    				{
    					while ( contern <= c )
    					{
    			            Aster = Aster + Aster;
    						contern++;
    					}
     
    					for ( int k=1; k <= l; k++)
    					{
    			            System.out.println( Aster );	        	    
    			        }
    				}	
    				else 
    				    if ( c == 1 )
    				    {
    				        for ( int k=1; k <= l; k++)
    					    {
    			                System.out.println( Aster );
    				        }
    					}
    				else ;
     
     
    				Aster = "*";
     
            	}
     
    .
    .
    .

    I guess it was a sintax error. Programs works perfectly.
    Last edited by Norm; January 30th, 2013 at 12:27 PM. Reason: added / in code tag
    Andrew R.

Similar Threads

  1. What am I missing?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2012, 10:49 AM
  2. What am I missing?
    By prgmrGrl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 10th, 2012, 12:33 AM
  3. missing something so simple....help!
    By b094mph in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2011, 10:12 PM
  4. Missing drivers?
    By mjpam in forum JDBC & Databases
    Replies: 5
    Last Post: September 6th, 2010, 08:00 PM
  5. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM

Tags for this Thread