YesNoOk
avatar

-0 Java programming (Read 2056 times)

Started by ShunpoHadouken, August 25, 2009, 11:51:09 AM
Share this topic:
Re: -0 Java programming
#21  August 26, 2009, 12:47:16 PM
  • **
Yey the program now runs but still the problem exist  :(
sorry for asking too much.
still the output is zero instead of invalid.
Thanks For Any Help!!!
Re: -0 Java programming
#22  August 26, 2009, 01:40:50 PM
  • avatar
  • ******
What's your current code ?
If I struggled to the end of my determination, to the end of my way of life with my followers, if the result is ruin, then this ruin is inevitable. Grieve. Shed tears. But you cannot regret.
Re: -0 Java programming
#23  August 26, 2009, 01:59:13 PM
  • **
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");}
}
}
}
Thanks For Any Help!!!
Re: -0 Java programming
#24  August 26, 2009, 03:19:25 PM
  • avatar
  • ******
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.
If I struggled to the end of my determination, to the end of my way of life with my followers, if the result is ruin, then this ruin is inevitable. Grieve. Shed tears. But you cannot regret.
Re: -0 Java programming
#25  August 26, 2009, 03:36:04 PM
  • ******
  • 日本は素晴らしい国です。
maybe you should try charat(number) instead, since its more straight forward?
Re: -0 Java programming
#26  August 26, 2009, 03:40:21 PM
  • ******
    • Germany
INDENTATION.

learn it, use it. now.

Also: if(i.equals("-0"))
Re: -0 Java programming
#27  August 26, 2009, 11:53:58 PM
  • **
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.
Thanks For Any Help!!!
Re: -0 Java programming
#28  August 26, 2009, 11:59:29 PM
  • ******
  • 日本は素晴らしい国です。
Quote
Why 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.


Quote
Why 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.
Re: -0 Java programming
#29  August 27, 2009, 12:06:34 AM
  • **
Oh so that was it.
thank you.

Thanks Iced,Valodim,Byakko,Cybaster, thanks a lot for help!.  ;D
Thanks For Any Help!!!