Hi i need help. Our professor gave an assignment i thought is easy, but not.its about Java programming language with the usage of Net beans 5.0.its the usage of IF_ELSE.the output is when you input a number, it will determine if the inputted number is positive(greater than 0), negative(lesser than 0), Zero(0) or invalid(if inputted is -0).everything is alright but still have a problem,when i inputted "-0" it displays "0" instead of "invalid".What should I do?i'm trying to make everything alright for almost 3 hrs but i failed.can anyone help me out please.thanks in advance.
put it in an array at first to test.If word[0]==´-´&& word[1]=='0'return invalidelse start your other tests here.
thanks but i don't understand anyway, here's what i used to do:class Switch { public static void main(String[] args) {double i = -0;if (i>0){System.out.println("POSITIVE");}else if (i<0) {System.out.println("NEGATIVE");} else if (i==+0) {System.out.println("ZERO");} else if (i==-0.0) {System.out.println("INVALID");} else {System.out.println("INVALID");} }}still doesn't work.
string x=i.tostring();if(x[0]=="-"&&x[1]=="0"){System.out.println("INVALID");else{if (i>0){System.out.println("POSITIVE");}else if (i<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}}geddit?
-0 is most likely simplified to 0 as soon as you put it as a number, because -0 = 0. Like, if you declare a number (integer, double or whatever), even if you set it to "-0", if you try to display it immediately afterward it will still give you 0 and not -0.Declare it as a string right off the bat and test for -0 then you can make it a number, I don't even reckon the number made tostring will give you the minus back.You can check by writing i = -0 and then putting a sysout to display it, it will give you 0. Not +0, not -0, just 0.
Iced said, August 25, 2009, 01:02:56 pmstring x=i.tostring();if(x[0]=="-"&&x[1]=="0"){System.out.println("INVALID");else{if (i>0){System.out.println("POSITIVE");}else if (i<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}}geddit?Am i right ???class Switch { public static void main(String[] args) {int i = -0;string x=i.tostring();if(x[0]=="-"&&x[1]=="0"){System.out.println("INVALID");}else{if (i>0){System.out.println("POSITIVE");}else if (i<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}} }}i dunno why it don't run.Byakko said, August 25, 2009, 01:09:47 pm-0 is most likely simplified to 0 as soon as you put it as a number, because -0 = 0. Like, if you declare a number (integer, double or whatever), even if you set it to "-0", if you try to display it immediately afterward it will still give you 0 and not -0.Declare it as a string right off the bat and test for -0 then you can make it a number, I don't even reckon the number made tostring will give you the minus back.You can check by writing i = -0 and then putting a sysout to display it, it will give you 0. Not +0, not -0, just 0.Yes you're right.it gives me only 0. ???
You're not doing what Byakko said :Byakko said, August 25, 2009, 01:09:47 pmDeclare it as a string right off the bat and test for -0 then you can make it a number- Declare i as a string. and set i to the value you want.- Then test if the string is -0 or not. * If it's -0, invalid. * If it's not -0, convert it to an int and do your else_ifs.
Oh totally, you need to have that as a string from the getgo, byakko was right, I didnt even notice that.If you declare it as a int or double it will eat up the - or +you are gonna have to do the other way around when testing them, and do a i.toint()
im really sorry im a beginner heres what i did:public class Switch { public static void main(String[] args) {string i= -0;string x=i.toint();if(x[0]=="-"&&x[1]=="0"){System.out.println("INVALID");}else{if (i>0){System.out.println("POSITIVE");}else if (i<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}} }}but i got these error:init:deps-jar:Compiling 2 source files to F:\NetBeans\Switches\build\classesF:\NetBeans\Switches\src\switches\Switch.java:3: cannot resolve symbolsymbol : class string location: class Switchstring i= -0;F:\NetBeans\Switches\src\switches\Switch.java:4: cannot resolve symbolsymbol : class string location: class Switchstring x=i.toint();2 errorsBUILD FAILED (total time: 0 seconds)
?? Why do you do x = i.toint() ? You just declared x as a String.Just go directly with if(i[0]=="-"&&i[1]=="0") without any toint before. You check if it's -0, then you declare an int and convert i, and then you test that int.Read this again :Cybaster said, August 25, 2009, 01:44:53 pm- Declare i as a string. and set i to the value you want.- Then test if the string is -0 or not. * If it's -0, invalid. * If it's not -0, convert it to an int and do your else_ifs.
public class Switch { public static void main(String[] args) {string i= -0;if(i[0]=="-"&&i[1]=="0"){System.out.println("INVALID");}else{int x=i.toint();if (x>0){System.out.println("POSITIVE");}else if (x<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}} }}You need a string for the [] tests and a int for the >< tests
I'm really sorry i'm terrible at it.public class Switch { public static void main(String[] args) {string i= -0; if(i[0]=="-"&&i[1]=="0"){System.out.println("INVALID");}else{int x=i.toint();if (x>0){System.out.println("POSITIVE");}else if (x<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}} }}but net beans said:init:deps-jar:Compiling 1 source file to F:\NetBeans\Switches\build\classesF:\NetBeans\Switches\src\switches\Switch.java:3: cannot resolve symbolsymbol : class string location: class Switchstring i= -0; 1 errorBUILD FAILED (total time: 0 seconds)
Byakko said, August 25, 2009, 03:37:29 pmString i = "-0";Need quotes for strings.oh thanks, I'll remember that.i've change it to:public class Switch { public static void main(String[] args) {String i = "-0"; if(i[0]=="-"&&i[1]=="0"){System.out.println("INVALID");}else{int x=i.toint();if (x>0){System.out.println("POSITIVE");}else if (x<0) {System.out.println("NEGATIVE");} else {System.out.println("ZERO");}} }}but net beans said:init:deps-jar:Compiling 1 source file to F:\NetBeans\Switches\build\classesF:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String foundif(i[0]=="-"&&i[1]=="0")F:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String foundif(i[0]=="-"&&i[1]=="0")F:\NetBeans\Switches\src\switches\Switch.java:8: cannot resolve symbolsymbol : method toint ()location: class java.lang.Stringint x=i.toint();3 errorsBUILD FAILED (total time: 0 seconds)
Okay, it's time you get used to reading the debug message it gives you.F:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String found <= tells you where the error is - on line 4 of your file.=> Line 4 is :if(i[0]=="-"&&i[1]=="0") so the problem is in this line.F:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String found <= tells you what the error is.=> Bad Iced, telling you to do i[0] on a String. This notation works on an array, not on a string.You can either change i and declare it as an Array instead of a string, or you can change your test toi.length() >= 2 && i.substring(0,1) == "-" && i.substring(1,2) == "0"Reading the rest of the error message will tell you other things too, by the way, things you'll also have to take care of.
Quote=> Bad Iced, telling you to do i[0] on a String. This notation works on an array, not on a string.You can either change i and declare it as an Array instead of a string, or you can change your test toi.length() >= 2 && i.substring(0,1) == "-" && i.substring(1,2) == "0"OOopps. Must have thought C , where strings can be acessed like arrays. Cant you access characters on strings by index number as well with another fuction? Dont have Eclipse at the office right now.for the conversion of the string into a int, try to usestring i="433"int numx = Integer.parse(i);
Integer.parseInt(i), not parse.QuoteCant you access characters on strings by index number as well with another fuction?Right, you can use (i.charAt(0) == "-" && i.charAt(1) == "0").
tell your teacher he's an idiot. -0 = +0 = 0, treating these any different just doesn't make any sense
Don't be practical, it's a school exercise, half of them don't make sense and I am pretty sure the teacher is loling at his students trying to give them that excuse, that -0 is what makes them think that maybe they should treat the input differently to just converting it to an int.
Its a practical thinking exercise, you got a starting point and an end point and you need to interpret the numbers not as numbers but as data. That leap is what matters on the exercise, when you stop treating them as numbers and start thinking of them as data.