ebk
ebk

Reputation: 11

How to use OR in Java If statement

Beginner Java coder here, I have a simple Scanner application that takes user input. I want to put an if statement to prevent the user from putting negative values into the scanner, or at least get an error message when they do so.

I have tried to put an if statement that if the number > 0 then the code executes, else displays an error message. This seems to work when testing for x, but not for x OR y.

  if (x >= 0 || y >= 0){
  System.out.println("Sum of x+y = " + z);

  if (x == y) {
      System.out.println("x and y are equal");
  } else {
      System.out.println("x and y are not equal");

  System.out.println("Counting from 0 to " + x);
  for (int i = 0; i <= x; i++) {
      System.out.print(i + " ");
     }

Upvotes: 0

Views: 153

Answers (4)

bjaklic
bjaklic

Reputation: 59

A better way to continuously prevent the user from inputting negative numbers would be:

Scanner in = new Scanner(System.in);
int x, y;

do {
    System.out.println("Input x:");
    x = in.nextInt();
    System.out.println("Input y:");
    y = in.nextInt();
} while (x < 0 || y < 0);

This do-while loop will ask for inputs until both x and y are positive integers. I'm not sure if this is what you wanted to achieve but that's what I gathered from your question since you said you wanted to use an if statement to prevent negative input.

Other than that, you're missing closing curly brackets like a couple people said already, but your code snippet looks incomplete. For future reference, please include more of your code for people to work from and/or the exceptions you get when trying to run your code.

Upvotes: 0

Bwalya
Bwalya

Reputation: 118

I recommend you use else if as opposed to 2 'if's, for example

        If (x >= 0 || y >= 0){
        //your code
        } else if (x == y) {
        //your code 
       } else {
        //your code 
      }

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

You seem to be posting a code fragment, because the brackets don't match up. Plus, you don't show any code that prints an error message.

But the main point of your question seems to be about a logic problem, not a syntax error with the code. So I'll pretend the syntax errors aren't there (although you do need to fix them). The logic problem seems to be here:

if (x >= 0 || y >= 0) {
    // all is good
} else {
    // print error message
}

This test will succeed if either x or y is non-negative. From your description, you want the test to fail if either is negative. For that, you need to either reverse the sense of the tests:

if (x < 0 || y < 0) {
    // print error message
} else {
    // all is good
}

or else use AND instead of OR:

if (x >= 0 && y >= 0) {
    // all is good
} else {
    // print error message
}

Upvotes: 4

GitPhilter
GitPhilter

Reputation: 171

If you want both numbers to be >=0 you should use AND instead of OR. If x is -5 but y is 7, the code will execute, but your counting from 0 to -5 will be pointless.

Upvotes: 3

Related Questions