Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

int problem

  Asked By: Leona    Date: Aug 08    Category: Java    Views: 686
  

i was testing the use of if and else statements in java and come across a really
awkward situation
my code
----------------------------------xxxxxxxxxxxxxxxxxx----------------------------\
-------------
public class gear
{
public static void main(String args[])
{
int i=0;
if (i=0)
{
System.out.println("zero");
}
else
{
System.out.println("test");
}
}
}
----------------------------------xxxxxxxxxxxxxxxxxx----------------------------\
-------------
whenever i compile the above program i get the compilation error : -
gear.java:6: incompatible types
found : int
required: boolean
if (i=0)
^
i changed the above code to : -
----------------------------------xxxxxxxxxxxxxxxxxx----------------------------\
-------------
public class gear
{
public static void main(String args[])
{
int i=0;
//changed the if statement to not equal to
if (i!=0)
{
System.out.println("zero");
}
else
{
System.out.println("test");
}
}
}
----------------------------------xxxxxxxxxxxxxxxxxx----------------------------\
-------------
it compiled without errors and displayed
test
I'm writing a program for handling numbers and cannot understand the mess.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Funsani Chalthoum     Answered On: Aug 08

it should be

if(i == 0)
{
//sop
}

 
Didn't find what you were looking for? Find more on int problem Or get search suggestion and latest updates.




Tagged: