• Register
2 votes


Hello all,
I am a noob to this java programming. I started learning it a few days back. Re-type the code and fix any errors. the code should convert non-positive numbers to 1. As long as I understand this problem is sying to convert a negative number to positive. So, I am struggling with a problem like converting a negative number to 0 or 1 in my program. I’ve got a shallow concept in my mind but that’s not enough for my case. If your understanding is up to this level, please pull me up from the grave LOL.



12 7 7
15,250 points

2 Answers

0 votes


Hello Gavin,

You’ve asked an interesting but very basic question. No shame! I appreciate your asking. You can solve this problem by using an if-statement in your program. If-else will check your number and then return it as you wanted. Let’s start:

public class NegToPos {
    public static void main (String [] args) {
        int Num = 0;
        if (Num >= 0)
            System.out.println("It's a non negative number");
            System.out.println("It's a negative nummber; now converting to 1");
            Num = 1;
            System.out.format("Final: %d", Num);

So, this code should convert non-positive numbers to 1. Thanks again. Keep asking.

13 9 6
94,240 points
0 votes
Thanks for answer was really helpful
0 points

Related questions

0 votes
1 answer 343 views
Problem : I am facing issues with following Java code and unable to figure out the cause behind the issue, I guess it is some logical error but I am unable to figure it out, my all results are ending up as 0. Please find below the Java Code: import java.util.Scanner; ... out.format("Final Result: %d", myUserNum); System.out.println(""); return; } } Please let me know how can I fix this issue?
asked Dec 3, 2019 alecxe 7.5k points
0 votes
1 answer 112 views
Problem : I am very new to r programming. I have the array and I want to implement the below method: x<-function(b){ if (b>0){ b/sum(b) } else 1 } This method is written to check if there is any value which is larger than the 0 and if it says ... message: &ldquo;Warning message: r the condition has length > 1 and only the first element will be used&rdquo;. How can I fix the above error message?
asked Feb 24 mphil 2.3k points
0 votes
1 answer 18 views
Problem: I am very new to the Java. I am currently trying to understand the if statements in java. I have one doubt as follows if(a && b || c || d || e) then whether it will check (a && b) if a and b must be always true then only will it allow inside ... a && e))here if any of the conditions are true then will it allow inside ? Please guide me in solving my doubts related to if statements in java.
asked Jul 29 Raphael Pacheco 4.9k points
1 vote
1 answer 27 views
Problem: Let me put it as sim as I can. I have an if statement in my code and it returns a value. Since IF is a boolean statement it should return value whether it&rsquo;s true or false. So, in my program when a method tests an argument and returns a true or ... as a parameter in a different method inside an IF statement. How should I do that? Any kind of help is appreciated by my side. Thanks.
asked Mar 17 Gavin 15.3k points
0 votes
1 answer 7 views
Problem: Hello Kodlogs, i am trying to convert decimal number to binary number .Is there any built in module/function in python so that i can use that for my conversion.Can any one help me with this? Thanks in advance
asked Oct 8 kalyani456 280 points