0

trying to terminate program using negative numbers and if else statement . does anyone see whats wrong with this thanks.

import java.util.Scanner;

public class Assignment {
    public static void main(String args[]){
    int n;
    int i=0;

    System.out.print("Enter a Number:");
    Scanner scanner = new Scanner(System.in);
    n= scanner.nextInt();
    int backUp = n;
    if(n>0)

        n=n/10;
        i++;

        else if(backUp = -1)

        System.out.print("program terminated......");
            System.exit(0);



    System.out.println("Number of Digits in " +backUp +" is " +i);

    }
}
2
  • 3
    You should use brackets around the if and else blocks { and } Commented Oct 9, 2013 at 8:40
  • I wonder why you don't use IDE 'cause it seems your code couldn't be even compiled successfully. I see you asked what's wrong with it..so compiler would answer :) and then we could discuss the logic. Commented Oct 9, 2013 at 9:23

6 Answers 6

6

First of all, = is for assigning values. Use == for comparing.

Also, you need to use {} after if and else statements if you want to run more than one line.

Sign up to request clarification or add additional context in comments.

5 Comments

@javaBeginner It is. If you look at the if(n>0) part, only the very next line will be dependent on the if statement, the next line (i++) will always execute, then the else if will result in a syntax error because it's not immediately after an if statement.
Without {} the else has no meaning. And I think System.exit(0) should be called when exiting. (-1)
i mean to say that when user enters any value greater than 0 then it will be terminated
@javaBeginner The intention seems to be that the user entering -1 will result in the program terminating, but any other input wouldn't.
@javaBeginner While technically you may be correct, it is always good practice to wrap your if statements in {}, prevents silly mistakes like the one presented - IMHO
4
   else if(backUp =  -1)

Should be

   else if(backUp == -1)

= assignment operator , == is for comparing

And finally with missed {}

  if (n > 0) {
            n = n / 10;
            i++;
   } else if (backUp == -1) {
            System.out.print("program terminated......");
            System.exit(0);
   }else{
          // do something else. I have no idea. 
        }

Comments

2

You are missing { } for your if-statements. In if statements without the { }, only the line following the if-statement will be affected by the outcome of the if-test.

So:

if (condition)
    doSomething();
    doSomethingElse();

will execute doSomething() if condition == true and doSomethingElse() no matter if condition == true.

if (condition) {
    doSomething();
    doSomethingElse();
}

will execute both doSomething() and doSomethingElse(), if and only if condition == true.

Comments

1

You are using an assignment operator to evaluate a condition.

else if(backUp = -1)

should be

else if(backup == -1)

Comments

1

remove else use if(backup==-1).

3 Comments

There is a difference between if and if else.
@AnubhavSharma Then tell him what the proper syntax is, don't tell him to completely change the way his code will execute.
it should be like this...if(n>0) { n=n/10; i++; } else if(backUp == -1)
1

First of all your indenting.

Secondly, if you want to execute multiple statements given a certain condition you'll need to put it in a code block like if(x) { /* do multiple things */ }.

Thirdly, your else if(backUp = -1) is invalid because you need a boolean expression inside a if, backUp = -1 is an assignment and thus does not evaluate to a boolean (you probably want backUp == -1).

And you probably want to loop the n = n/10; i++; part because now it will never count more than 1 digit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.