YesNoOk
avatar

-0 Java programming (Read 2281 times)

Started by ShunpoHadouken, August 25, 2009, 11:51:09 am
Share this topic:
-0 Java programming
#1  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.
Thanks For Any Help!!!
Re: -0 Java programming
#2  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.
Re: -0 Java programming
#3  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.
Thanks For Any Help!!!
Re: -0 Java programming
#4  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?
Re: -0 Java programming
#5  August 25, 2009, 01:09:47 pm
  • avatar
  • ******
-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.
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
#6  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.  ???

Thanks For Any Help!!!
Re: -0 Java programming
#7  August 25, 2009, 01:44:53 pm
  • ******
  • Limited time to use Infinite power !
    • France
    • network.mugenguild.com/cybaster/
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.
Re: -0 Java programming
#8  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()
Re: -0 Java programming
#9  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)

Thanks For Any Help!!!
Re: -0 Java programming
#10  August 25, 2009, 02:41:54 pm
  • avatar
  • ******
?? 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.
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.
Last Edit: August 25, 2009, 02:44:55 pm by Byakko
Re: -0 Java programming
#11  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
Re: -0 Java programming
#12  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)
Thanks For Any Help!!!
Re: -0 Java programming
#13  August 25, 2009, 03:37:29 pm
  • avatar
  • ******
String i = "-0";
Need quotes for strings.
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
#14  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)

Thanks For Any Help!!!
Re: -0 Java programming
#15  August 25, 2009, 03:55:25 pm
  • avatar
  • ******
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.
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.
Last Edit: August 25, 2009, 04:00:45 pm by Byakko
Re: -0 Java programming
#16  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);
Re: -0 Java programming
#17  August 25, 2009, 05:12:25 pm
  • avatar
  • ******
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").
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
#18  August 25, 2009, 07:10:46 pm
  • ******
    • Germany
tell your teacher he's an idiot. -0 = +0 = 0, treating these any different just doesn't make any sense --;
Re: -0 Java programming
#19  August 25, 2009, 07:16:49 pm
  • ******
  • [E]
    • Mexico
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.
Re: -0 Java programming
#20  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.