The Mugen Fighters Guild

Help => Off-Topic Help => Topic started by: ShunpoHadouken on August 25, 2009, 11:51:09 AM

Title: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 11:51:09 AM
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.
Title: Re: -0 Java programming
Post by: Iced on August 25, 2009, 11:59:59 AM
put it in an array at first to test.

If word[0]==´-´&& word[1]=='0'
return invalid
else
 start your other tests here.
Title: Re: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 12:52:43 PM
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.
Title: Re: -0 Java programming
Post by: Iced on August 25, 2009, 01:02:56 PM
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?
Title: Re: -0 Java programming
Post by: Byakko on 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.
Title: Re: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 01:30:27 PM
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?

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.

-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.  ???

Title: Re: -0 Java programming
Post by: Cybaster on August 25, 2009, 01:44:53 PM
You're not doing what Byakko said :
Declare 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.
Title: Re: -0 Java programming
Post by: Iced on August 25, 2009, 01:52:05 PM
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()
Title: Re: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 02:37:12 PM
im really sorry im a beginner  :sugoi:

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\classes

F:\NetBeans\Switches\src\switches\Switch.java:3: cannot resolve symbol

symbol  : class string

location: class Switch

string i= -0;

F:\NetBeans\Switches\src\switches\Switch.java:4: cannot resolve symbol

symbol  : class string

location: class Switch

string x=i.toint();

2 errors

BUILD FAILED (total time: 0 seconds)

Title: Re: -0 Java programming
Post by: Byakko on August 25, 2009, 02:41:54 PM
?? 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 :
- 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.
Title: Re: -0 Java programming
Post by: Iced on August 25, 2009, 02:44:51 PM
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
Title: Re: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 03:32:13 PM
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\classes

F:\NetBeans\Switches\src\switches\Switch.java:3: cannot resolve symbol

symbol  : class string

location: class Switch

string i= -0;

1 error

BUILD FAILED (total time: 0 seconds)
Title: Re: -0 Java programming
Post by: Byakko on August 25, 2009, 03:37:29 PM
String i = "-0";
Need quotes for strings.
Title: Re: -0 Java programming
Post by: ShunpoHadouken on August 25, 2009, 03:47:45 PM
String 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\classes

F:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String found

if(i[0]=="-"&&i[1]=="0")

F:\NetBeans\Switches\src\switches\Switch.java:4: array required, but java.lang.String found

if(i[0]=="-"&&i[1]=="0")

F:\NetBeans\Switches\src\switches\Switch.java:8: cannot resolve symbol

symbol  : method toint ()

location: class java.lang.String

int x=i.toint();

3 errors

BUILD FAILED (total time: 0 seconds)

Title: Re: -0 Java programming
Post by: Byakko on August 25, 2009, 03:55:25 PM
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 to
i.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.
Title: Re: -0 Java programming
Post by: Iced on August 25, 2009, 04:30:13 PM
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 to
i.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 use
string i="433"
int numx = Integer.parse(i);
Title: Re: -0 Java programming
Post by: Byakko on August 25, 2009, 05:12:25 PM
Integer.parseInt(i), not parse.

Quote
Cant you access characters on strings by index number as well with another fuction?
Right, you can use (i.charAt(0) == "-" && i.charAt(1) == "0").
Title: Re: -0 Java programming
Post by: Valodim on August 25, 2009, 07:10:46 PM
tell your teacher he's an idiot. -0 = +0 = 0, treating these any different just doesn't make any sense --;
Title: Re: -0 Java programming
Post by: Bastard Mami on August 25, 2009, 07:16:49 PM
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.
Title: Re: -0 Java programming
Post by: Iced on August 26, 2009, 10:29:02 AM
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.
Title: Re: -0 Java programming
Post by: ShunpoHadouken on 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.
Title: Re: -0 Java programming
Post by: Byakko on August 26, 2009, 01:40:50 PM
What's your current code ?
Title: Re: -0 Java programming
Post by: ShunpoHadouken on 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");}
}
}
}
Title: Re: -0 Java programming
Post by: Byakko on August 26, 2009, 03:19:25 PM
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.
Title: Re: -0 Java programming
Post by: Iced on August 26, 2009, 03:36:04 PM
maybe you should try charat(number) instead, since its more straight forward?
Title: Re: -0 Java programming
Post by: Valodim on August 26, 2009, 03:40:21 PM
INDENTATION.

learn it, use it. now.

Also: if(i.equals("-0"))
Title: Re: -0 Java programming
Post by: ShunpoHadouken on 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.
Title: Re: -0 Java programming
Post by: Iced on 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.
Title: Re: -0 Java programming
Post by: ShunpoHadouken on 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