-0 Java programming

Started by ShunpoHadouken, August 25, 2009, 11:51:09 AM

Previous topic - Next topic

ShunpoHadouken

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!!!

Byakko

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.

ShunpoHadouken

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!!!

Byakko

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.

Iced

maybe you should try charat(number) instead, since its more straight forward?

Valodim

INDENTATION.

learn it, use it. now.

Also: if(i.equals("-0"))

ShunpoHadouken

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!!!

Iced

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.

ShunpoHadouken

Oh so that was it.
thank you.

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