0

I am not getting errors at all in my code but it is NOT doing what I want it to - at all. I understand that there is another question related to divisibility to 3 or 5 but I do not think the question relates, as our coding is built completely different. I also find that this answer, this other answer, or even this answer are not helping me, either.

Anyway, I am trying to use a simple boolean expression to determine if an expression is true or not, then if it is true or not printing different lines. The intended look I would get is something like:

3 is divisible by 3
3 is not divisible by 5
4 is not divisible by 3
4 is not divisible by 5
5 is not divisible by 3
5 is divisible by 5
and so on...

However, I'm getting this:

3 is divisible by 3
3 is not divisible by 3
3 is divisible by 5
3 is not divisible by 5
and so on...

Initial problem can be found here. Right now I am just experimenting with the code, trying to get myself to a better understanding of Java. I wanted to start by knowing how to separate integers that are divisible by the numbers from integers that are not divisible by the numbers and I figured the best way to test this was to print the lines out. I do not know if I am going in the right direction at ALL here. Can anyone either give me pointers on the current code I have, or some hints, such as links to the corresponding Java commands that I will have to use for this program to work. If you post code that works, so I can see what I should be doing, that is fine too but please explain it to me, then. I don't want to just know what to type, I am trying to develop a solid foundation of programming problem-solving.

Sorry if this is a terrible question, I am EXTREMELY new to programming in general and am completely self-teaching. Give me some pointers, and I can most definitely change the question!

package multiplesof3and5;

public class InitialProgram {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int integer1;
        integer1 = 0;
        boolean divisibleby3 = (integer1 % 3) == 0;
        boolean divisibleby5 = (integer1 % 5) == 0;

           for( integer1=0; integer1<1000; integer1++){
            if(divisibleby3 = true);{
                System.out.println(integer1 + " can be divided by 3");} 
            if(divisibleby3 = false);{
                System.out.println(integer1 + " cannot be divided by 3");}
            if(divisibleby5 = true);{
                System.out.println(integer1 + " can be divided by 5");}
            if(divisibleby5 = false);{
                System.out.println(integer1 + " cannot be divided by 5");
            }

        }



    }   
}
Community
  • 1
  • 1
Ben Third
  • 11
  • 1
  • 1
  • 1

3 Answers3

1

To start:

if(divisibleby3 = true);{

has a stray semicolon. Secondly, = is an assignment, but rather == should be used for comparison, and is not even necessary for booleans. Use this format:

if(divisibleby3){

and

if(!divisibleby3){ // if not divisible by three

Secondly,

boolean divisibleby3 = (integer1 % 3) == 0;
boolean divisibleby5 = (integer1 % 5) == 0;

need to be inside the loop, since otherwise they're only evaluated once (you only check if 0 is divisible by three and five and use the result of that check over and over again, instead of checking each integer).

Optionally, instead of using if/if pairs, you can just use if/else twice in each loop (if divisible by 3 print that it is, else print that it isn't; same for divisibility by five).

In the end, this is the code that will work:

public static void main(String[] args) {
      for(int integer1 = 0; integer1 < 1000; integer1++){
        boolean divisibleby3 = (integer1 % 3) == 0;
        boolean divisibleby5 = (integer1 % 5) == 0;
        if(divisibleby3) {
            System.out.println(integer1 + " can be divided by 3");
        } else {
            System.out.println(integer1 + " cannot be divided by 3");
        }
        if(divisibleby5){
            System.out.println(integer1 + " can be divided by 5");
        } else {
            System.out.println(integer1 + " cannot be divided by 5");
        }

    }



}   
nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

You're detemining your divisibility before you start to check it against numbers:

int integer1;
integer1 = 0;
boolean divisibleby3 = (integer1 % 3) == 0;
boolean divisibleby5 = (integer1 % 5) == 0;

   for( integer1=0; integer1<1000; integer1++){

You need to continuously update your divisibility with each change of integer1, like so:

for( integer1=0; integer1<1000; integer1++){        
    boolean divisibleby3 = (integer1 % 3) == 0;
    boolean divisibleby5 = (integer1 % 5) == 0;

Just noticed @hexafraction's answer:

You also need to compare statements with ==, right now you're just assigning the booleans to true or false, since you only use =

Jeeter
  • 5,887
  • 6
  • 44
  • 67
0

The issue in your code is that you never change your booleans. They always stay the same, because they are only based on the initial integer1. To fix this, simply move both of your boolean declarations into the for loop. I am on mobile, so I can't write it down / check it, but I am pretty certain it'll work. Best regards, -Yuval Gat

Yuval Gat
  • 103
  • 6