Yey the program now runs but still the problem exist sorry for asking too much.still the output is zero instead of invalid.
Here public class Switch { public static void main(String[] args) {String i = "-0"; if(i.length() >= 2 && i.substring(0,1) == "-" && i.substring(1,2) == "0"){System.out.println("INVALID");}else{int x=Integer.parseInt(i);if (x>0){System.out.println("POSITIVE");}else if (x<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}}}}
That's odd. After "String i = -0" and before the if test, try adding a println to display the values of "i.length()", of "i.substring(0,1)" and of "i.substring(1,2)", I might have messed up a number when I gave you that.
public class Switch { public static void main(String[] args) {String i = "-0"; if(i.equals("-0")){System.out.println("INVALID");}else{int x=Integer.parseInt(i);if (x>0){System.out.println("POSITIVE");}else if (x<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}}}}Yehey! the program now works perfectly.Thank you very much everyone that helped me.thanks a lot.Now this program is finished.but, can i ask some last question. Please. I need this so that i can explain it to my professor when she ask me.Why should I declare variable "i" as string? String i = "-0"; Is it so that java will read "-0" different form a "0".Why should i declare variable "x" like this: int x=Integer.parseInt(i);? Is it to convert the string variable i to an integer?thnaks a lot.
QuoteWhy should I declare variable "i" as string? String i = "-0"; Is it so that java will read "-0" different form a "0".Because if declared as a int, java will drop the signal and assume you meant zero only.So yes, if it was int i=-0, java would only read 0.QuoteWhy should i declare variable "x" like this: int x=Integer.parseInt(i);? Is it to convert the string variable i to an integer?so that you convert the string into a integer, in case it wasnt -o.If the value was coming from outside and you werent able to know what it was, it would help to have you test for the -0 first, then if it passes that test, you turn it into an integer ( parse it into integer) and continue testing the values.