Aramza
Aramza

Reputation: 193

Java Convert negative numbers to 0

I am having issues with this code and can't figure out the issue, I know it's a logical error but I can't seem to figure it out, all results end up as 0.

Code:

import java.util.Scanner;

public class ConvertNegative {
   public static void main (String [] args) {
      int userNum = 0;

      if (userNum >= 0)
         System.out.println("Non-negative");
      else
         System.out.println("Negative; converting to 0");
         userNum = 0;
         System.out.format("Final: %d", userNum);
         System.out.println(""); 

      return;
   }
}

I'd appreciate any help, thanks.

Upvotes: 2

Views: 5828

Answers (4)

asiby
asiby

Reputation: 3389

Beauty comes in simplicity.

You will get a better code by simply sanitizing the userNum variable in one line like so ....

class Main {
  public static void main(String[] args) {
    int userNum = -5;

    userNum = Math.max(userNum, 0);

    System.out.println("userNum = " + userNum);
  }
}

The key is to use the Math.max() function to do the job.

Upvotes: 6

Travis M Whitton
Travis M Whitton

Reputation: 1

You need to re-write the code by adding the correct braces and separating the else from the bottom print commands.

 if (userNum >= 0) {
     System.out.println("Non-negative");
 }
 else {
    System.out.println("Negative; converting to 0");
    userNum = 0;
   }
 System.out.format("Final: %d", userNum);
 System.out.println("");
   ```

Upvotes: -1

SM ANSARI
SM ANSARI

Reputation: 385

You have missed curly braces in else block. you have to give curly braces when your else has more than one statements. also you didn't get the value from the user. As main method is void, there is no need of return statement.

Hope following code will help you

import java.util.Scanner;

public class ConvertNegative {
   public static void main (String [] args) {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter a number....");
      int userNum = s.nextInt();

      if (userNum >= 0)
         System.out.println("Non-negative");
      else{
         System.out.println("Negative; converting to 0");
         userNum = 0;
         System.out.format("Final: %d", userNum);
         System.out.println(""); 
      }
   }
}

Upvotes: 0

Andreas Brunnet
Andreas Brunnet

Reputation: 732

If you have multi line conditions / for loops you need to set brackets. Without the brackets your else block would just execute the first statement after it. In your case the System.out.println("Negative; converting to 0");. In any case your variable userName would be set to 0 as the following line wouldn't be part of the else block anymore. More than 1 line = brackets.

public class ConvertNegative {
    public static void main (String [] args) {
        int userNum = 0;

        if (userNum >= 0)
            System.out.println("Non-negative");
        else{
            System.out.println("Negative; converting to 0");
            userNum = 0;
            System.out.format("Final: %d", userNum);
            System.out.println(""); 
        }
        //return; -> There is no need for the return as the main method has no return value.
    }
}

Upvotes: 6

Related Questions