Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyNewVector
{

public static void main(String[] args)
{
public void test(int x)
{
double odd = x%2;
if ((odd)
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
}
}

Gives the compliation errors
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable test
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
Type mismatch: cannot convert from double to boolean

at foo.MyNewVector.main(MyNewVector.java:9)
Can some one focus on this
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if ((odd)
{
System.out.println("odd");
}
else
{
System.out.println("even");
}

First you must finished parenthises )
Second for if condition compiler needs boolean expression but you use double.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyNewVector
{

public static void main(String[] args)
{
public void test(int x)
{
double odd = x%2;
if (odd)
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
}
}

Now also it gives me error of boolean where i need to include boolean i can't understand.
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean odd = true;
if ((odd))
{
System.out.println("odd");
}
else
{
System.out.println("even");
}

Now the program compile.
 
Alejandro Galvan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh:

Conditional and loop statements evaluates a boolean expression, but numeric, you can NOT, like in C or other languages, evaluate a condition with a number (false=0 or true!=0). So, you can NOT say:

You must use a boolean, or even a Boolean, which its only possible values are true or false, literally.
So, you do can say:


Hope this help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic