Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

rerurning a boolean value

Thread view: 
Danny Gopie - 07 Dec 2004 09:33 GMT
Hello, i want to make a problem to check if 2 numbers can be divided by each
other with a method
How can i process theis if i even cant return a boolean values in an int?

public class e32 {

   public static void main(String[] args) {

   int a = 6;
   int b = 3;
  int k =  (a % b == 0 || b % a == 0 );
boolean i = isVeelvoud(k);

// i get this error
//chapter4/e32.java [15:1] incompatible types
//found   : boolean
//required: int

}

   static boolean isVeelvoud(int a, int b){
   if (a == true){
                System.out.println(a);

          }
   return a;
   }
}
Heiner K?cker - 07 Dec 2004 10:25 GMT
> Hello, i want to make a problem to check if 2 numbers can be divided by each
> other with a method
[quoted text clipped - 24 lines]
>     }
> }

Many errors in your code.
Try this.

public class e32 {

   public static void main(String[] args) {

   int a = 6;
   int b = 3;
   boolean k =  ( ( a % b ) == 0 || ( b % a ) == 0 );
   boolean i = k;

   // the method isVeelvoud is not needed

Heiner Kuecker
Internet: http://www.heinerkuecker.de  http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Expression Language Parser: http://www.heinerkuecker.de/Expression.html
Danny Gopie - 07 Dec 2004 10:45 GMT
Thanks Keiner, I know it will work without the method and that its not
needed. But for school i need to write a method for this program, thats
why..

Danny

>> Hello, i want to make a problem to check if 2 numbers can be divided by
>> each
[quoted text clipped - 45 lines]
> http://www.control-and-command.de
> Expression Language Parser: http://www.heinerkuecker.de/Expression.html
Gordon Beaton - 07 Dec 2004 11:19 GMT
> Thanks Keiner, I know it will work without the method and that its
> not needed. But for school i need to write a method for this
> program, thats why..

Then write a method that takes integer values a and b, and returns the
result of the comparison, a boolean.

In the example you posted, you are doing all of the work in the main
method, leaving isVeelvoud() virtually nothing to do.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Andrew Thompson - 07 Dec 2004 11:30 GMT
> How can i process theis if i even cant return a boolean values in an int?

<sscce>
/* ClassNames start with captials, so this should be 'E32', but 'E' should
itself be a word so that would be 'Exercize32'.  OTOH, since we do not care
what exercise this is, you should do a global rename on it to something
sensible just before posting.  I suggest 'TestMethodValueReturn'. */
public class e32 {

   public static void main(String[] args) {

       //  please indent your code consistently with spaces
       int a = 6;
       int b = 3;
       int c = 7;
       // what was this mess suupposed to do?
       //int k =  (a % b == 0 || b % a == 0 );
       
       // the method now returns a boolean..
       boolean modIsZero = isVeelvoud(a,b);
       System.out.println( "a % b == 0? " + modIsZero);

       // ..which we can call with any parameters
       System.out.println( "a % c == 0? " + isVeelvoud(a,c) );    
   }

   /** To clarify that these attributes passed in the constructor
   are not the ones declare above, I changed the names. */
   static boolean isVeelvoud(int firstOperand, int secondOperand) {
       return (firstOperand%secondOperand==0);
   }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Gordon Beaton - 07 Dec 2004 12:08 GMT
> since we do not care what exercise this is, you should do a global
> rename on it to something sensible just before posting.

I don't agree with this advice, especially when it's given to a
beginner.

We don't really care what his class his called, and asking him to do a
global rename just before posting is asking him to post something
other than his real code, which creates a new set of potential
problems.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Andrew Thompson - 07 Dec 2004 14:22 GMT
>> since we do not care what exercise this is, you should do a global
>> rename on it to something sensible just before posting.
[quoted text clipped - 6 lines]
> other than his real code, which creates a new set of potential
> problems.

..hmmm.  Good point.  Scratch that last bit.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Starshine Moonbeam - 07 Dec 2004 17:16 GMT
> Hello, i want to make a problem to check if 2 numbers can be divided by each
> other with a method
[quoted text clipped - 7 lines]
>     int b = 3;
>    int k =  (a % b == 0 || b % a == 0 );
   
    k would be boolean here.

> boolean i = isVeelvoud(k);
>
[quoted text clipped - 13 lines]
>     }
> }

Signature

Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

jeffc - 07 Dec 2004 19:20 GMT
> Hello, i want to make a problem to check if 2 numbers can be divided by each
> other with a method
[quoted text clipped - 24 lines]
>     }
> }

You can return boolean values, but your code makes no sense.  k should be a
boolean.  You are calling isVeelvoud with 1 parameter, but takes 2 parameters.
Also in this function, you're comparing "a", an int, to "true", a boolean value.
You need to figure out the logic of your program and clarify exactly what you're
trying to do.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.