Home > Blockchain >  I am creating a java program but it is showing the error 'else' without 'if'
I am creating a java program but it is showing the error 'else' without 'if'

Time:02-20

class number
{
 public static void main(int a, int b, char c)
   { int r=0;
     if(c==' ')
     r= a b;  
     System.out.println("result is "  r);
    else if(c=='-')
     r= a-b; 
    System.out.println("result is "  r);
    else if(c=='/')
    r= a/b;
    System.out.println("result is "  r);
    else if(c=='%')
    r= a%b;
    System.out.println("result is "  r);
    else if(c=='*')
    r= a*b;
    System.out.println("result is "  r);
    else
    System.out.println("wrong operator");
}}

CodePudding user response:

The direct answer to your question is that you need braces around any multi-line statement within an if

if (c == ' ') {
    r = a   b;
    System.out.println("result is "   r)
} else if (c == '-') {
    r = a - b;
    System.out.println("result is "   r);
}

There are other issues with your code:

Java strongly recommends that class names start with an upper case letter

class Number {

and your main has the wrong signature, it must be

public static void main(String[] args) {

you then need to get your a, b and c values from args[0], args[1] and args[2]. You will need to convert the String arguments.

Strongly suggest you work through a Java tutorial, which will explain all this in detail.

CodePudding user response:

You can only write one statement within an "if" condition without braces. Since you have written 2 statements (i.e. the calculation and the printing) it will not compile.

In addition to that, the main method signature is incorrect as well and also make sure you use the Camel case naming convention while naming classes.

Take a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html for more details.

class Number { 
    public static void main(String[] args) { 
        // get inputs for c, a and b
        int r=0; 
        if(c ==' '){ 
            r= a b;
            System.out.println("result is "  r); 
        }
        else if(c=='-') {
            r= a-b; 
            System.out.println("result is "  r); 
        }
        else if(c=='/') {
            r= a/b; 
            System.out.println("result is "  r); 
        }           
        else if(c=='%') {
            r= a%b; 
            System.out.println("result is "  r);    
        }
        else if(c=='') {
            r= ab; 
            System.out.println("result is "  r); 
        }
        else 
            System.out.println("wrong operator"); 
    }
}

CodePudding user response:

See this ref :

   class number {
        public static void main(int a, int b, char c) {
            int r = 0;
            if (c == ' ') {
                r = a   b;
            } else if (c == '-') {
                r = a - b;
            } else if (c == '/') {
                r = a / b;
            } else if (c == '%') {
                r = a % b;
            } else if (c == '') {
                r = ab;
            } else {
                System.out.println("wrong operator");
            }
    
            System.out.println("result is "   r); //This line is commonly used, so we put it here.
        }
    }
  • Related