• Register
0 votes
550 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;
public class MyConvertNegative {
   public static void main (String [] args) {
      int myUserNum = 0;
      if (myUserNum >= 0)
         System.out.println("Poitive");
      else
         System.out.println("Negative; Converting To 0");
         myUserNum = 0;
         System.out.format("Final Result: %d", myUserNum);
         System.out.println(""); 
      return;
   }
}

Please let me know how can I fix this issue?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

In your code I can see that you have the multi line conditions so you need to set the brackets. Without them your else block will just execute the first line of the code after it. So in your case the line executed is  System.out.println("Negative; Converting To 0");. So in that case your variable myUserNum would be set to 0 as the next line will not be the part of the else block anymore. So you need to follow the More than 1 line = brackets.

public class MyConvertNegative {
    public static void main (String [] args) {
        int myUserNum = 0;
        if (myUserNum >= 0)
            System.out.println("Poitive");
        else{
            System.out.println("Negative; Converting To 0");
            myUserNum = 0;
            System.out.format("Final Result: %d", myUserNum);
            System.out.println(""); 
        }
        //return; -> No need for the return as your main function is not returning any value.
    }
}
9 7 4
38,600 points

Related questions

2 votes
2 answers 223 views
223 views
Problem: 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 ... but that’s not enough for my case. If your understanding is up to this level, please pull me up from the grave LOL. Thanks.
asked Mar 19, 2020 Gavin 15.3k points
0 votes
1 answer 58 views
58 views
Problem: Hi there! I am a noob and I have written following program: public class Negative{ public static void main(String []args){ int number = 5; System.out.println("The number before check is: " + number); if(number < 0) number = 0; else number = 0; System.out ... to 0. I have no clue what I am doing wrong. If there are any errors in my program please correct and mention it in comment. Thanks
asked Nov 27, 2020 Code Learner 9.2k points
0 votes
1 answer 3 views
3 views
Problem: Can somebody help me with the error I am getting.?
asked Mar 25 Sana8989 8.5k points
0 votes
1 answer 2 views
2 views
Problem: Please can anyone tell me the right procedure to do it? c# to vb net convert online
asked 5 days ago Ifra 24.4k points
0 votes
1 answer 2 views
2 views
Problem: I am stuck now .. need some suggestions .. please help me convert from c# to vb net
asked 5 days ago Ifra 24.4k points
0 votes
1 answer 48 views
48 views
Problem: How to I Solve this : python to c++ converter online HELP! I need help understanding this: python to c++ converter online Can someone please help solve this, I am new in python().
asked Mar 2 Mashhoodch 13k points
0 votes
1 answer 11 views
11 views
Problem: Can someone help me solve this: "Tomcat Started/Stopped with errors, return code: 1 Make sure you have Java JDK or JRE installed and the required ports are free. Check the "/xampp/tomcat/logs" folder for more information." I have installed Java JDK. Thanks
asked Mar 17 ummesalma 22.8k points
0 votes
1 answer 38 views
38 views
Please explain the following about "Cannot find symbol", "Cannot resolve symbol" or "Symbol not found" errors in JAVA?
asked Sep 28, 2020 Xaimax 9.5k points
1 vote
1 answer 83 views
83 views
Problem: For the following two lists: 1stlist = [-5, 10, -8, 16, -20] 2ndlist = [-24, 33, -55, -90, 11] I wrote a pyhton program where I can Count positive and negative numbers in this two lists : 1stlist = [-5, 10, -8, 16, -20] 2ndlist = [-24, 33, ... py_compile.PyCompileError: File "./prog.py", line 1 1stlist = [-5, 10, -8, 16, -20] ^ SyntaxError: invalid syntax How can I solve this problem?
asked Jun 1, 2020 samhaz 5k points
0 votes
1 answer 7 views
7 views
Problem: Do you have any suggestions about how I can resolve this &ldquo;Windows has stopped this device because it has reported problems. (code 43) how to fix&rdquo;?
asked Mar 30 tuhin1 47.2k points