Can some one help me with my programming project, it is supposed to loop. If the number is even output comes out even and vise-versa. Can someone please tell me what is wrong with my code.

Errors
"3 errors found:
File: C:\Users\Kaamil\evenodd test.java [line: 1]
Error: The public type NumberEvaluator must be defined in its own file
File: C:\Users\Kaamil\evenodd test.java [line: 3]
Error: Duplicate method evenOdd(int) in type NumberEvaluator
File: C:\Users\Kaamil\evenodd test.java [line: 8]
Error: Duplicate method evenOdd(int) in type NumberEvaluator"

Code.

public class NumberEvaluator
{
 public void evenOdd(int num)
 {

 }

 public void evenOdd(int num)
 {
  if (num % 2 == 0)
  {
    System.out.println ("even");
  }
  else
  {
    {
     System.out.println ("odd"); 
    }
  }
 }
 public static void main(String [] args)
 {
   int number;
   NumberEvaluator nE = new NumberEvaluator();
   nE.evenOdd(-5);
   nE.evenOdd(0);
   nE.evenOdd(7);
 }
}

Edited 4 Years Ago by GTech4life: wrong coding entered

The filename of the .java file should be the same as the name of the public class it contains.

You need to add a println that shows what number is being tested as well as the results of the test.

Edited 4 Years Ago by NormR1

sorry, somewhat hard to understand (im completly new at this) can you please show me the proper coding, and does that mean i save the file as NumberEvaluater or as evenOdd?

save the file as NumberEvaluater.java

Yes, the file should be named the same as the class name. Not the method name

ok so i saved it as NumberEvaluater but now the error has changed. It now says
"File: C:\Users\Kaamil\NumberEvaluator.java [line: 3]
Error: Duplicate method evenOdd(int) in type NumberEvaluator"

does this mean i should just delete line 3?

I just deleted line 3 and now the error is
"File: C:\Users\Kaamil\NumberEvaluator.java [line: 22]
Warning: The local variable number is never read"

Please help!?!?

Edited 4 Years Ago by GTech4life: added more

Rename one of the methods so its name is not the same as the other one's name.

Delete the definition of number if you do not use it.

Edited 4 Years Ago by NormR1

change the
" nE.evenOdd(-5);
nE.evenOdd(0);
nE.evenOdd(7);"

change each of those?

You must not define the method twice, and usually it is better practice to return value or set attribute of class, than print in function.

Comments
Might be too advanced for OP

what do i do to make this work?
and i have to use print as this is for school

it says

"File: C:\Users\Kaamil\NumberEvaluator.java [line: 22]
Warning: The local variable number is never read"

Remove line 22 where number is.

The message is a warning so the code should execute anyways if you were to try.

Edited 4 Years Ago by NormR1

i tried that, the compile completes without error but it dosent tell me wether the numbers (-5, 0, 7) are even or odd which is the main part of the whole code

I did the changes I mentioned and it compiles fine

I:\Java\code>javac NumberEvaluator.java

I:\Java\code>java NumberEvaluator
odd
even
odd

I:\Java\code>
This article has been dead for over six months. Start a new discussion instead.