0

i'm new in coding and tried to do some open university tasks. Maybe you guys can give some helping hand. I really even don't know how to start this task, witch is divide three Integers from main to method. Example 2, 10 where I should print 3, 6, 9 or 2, 6 where I should print 3, 6.

import java.util.Scanner;

public class divideByThree {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        divideByThree(2, 10);

    }
    public static void divideByThree(int start, int end) {

        while(true) {
            if (start % 3 == 0) {
                start++;
            }
            if (end % 3 == 0) {
                System.out.println(end);

            }
            System.out.println(start);
     }

    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
chigi05
  • 23
  • 2
  • Checkout this [post](https://stackoverflow.com/questions/6647296/how-to-check-if-an-integer-can-be-divided-by-3/6647324). – Doruk Eren Aktaş Jun 06 '20 at 15:21
  • Hi, can you please elaborate on the specifics of your problem? Are you getting results you aren't expecting to see? Do you get an error/exception that you don't know how to handle? – Yaron Grushka Jun 06 '20 at 15:21
  • Hi, my task is only: write method public static void divideBythree(int alku, int loppu), witch prints all numbers that are divide by three in given between. The numbers should be printed in order from smallest to largest. – chigi05 Jun 06 '20 at 15:25
  • Better to format your code with proper indentation and also read the community guidelines on how to ask a question before posting your question. – Varun Jain Jun 06 '20 at 15:25

5 Answers5

1

How about the following implementation about divideByThree method?

  public static void divideByThree(int start, int end) {
        for(int i = start; i <= end; ++i){
        if(i%3==0){
        System.out.print(i+" ");
        }
        }
    }
Partho KR
  • 112
  • 2
  • 10
0
  1. Do not use while(true) because it creates an infinite loop which will unnecessarily make your program complex.
  2. You need to increase start by 1 in each iteration and terminate the loop when the value of start value goes beyond end.
  3. Whenever start % 3 == 0 becomes true, print start.

Given below is a sample code which you can use to understand the points mentioned above.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter the start number: ");
        int start = reader.nextInt();
        System.out.print("Enter the end number: ");
        int end = reader.nextInt();
        divideByThree(start, end);
    }

    public static void divideByThree(int start, int end) {
        while (start <= end) {
            if (start % 3 == 0) {
                System.out.println(start);
            }
            start++;
        }
    }
}

A sample run:

Enter the start number: 3
Enter the end number: 9
3
6
9
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Recursive Approach

import java.util.Scanner;

public class DivideByThree {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int start = reader.nextInt();
        int end = reader.nextInt();
        divideByThree(start, end);
        reader.close();
    }

    public static void divideByThree(int start, int end) {
        if(start % 3 == 0) {
            System.out.println(start);
        }
        if( start <= end) {
            divideByThree(++start, end);
        }  
    }
}
Cortex
  • 585
  • 3
  • 19
  • 1
    As of now, OP doesn't seem to be so mature to understand recursion. However, this is a nice answer and can be useful to him in future or anyone else in general. – Arvind Kumar Avinash Jun 06 '20 at 16:04
  • Right, I thought since it's university level exercise it could be aimed to understand a specifc fundamental rather than using any arbitrary looping so decided to throw a recursion application. – Cortex Jun 06 '20 at 16:07
0

The while(true) is an endless loop (something you would want to avoid). It will only stop if you add a break statement somewhere inside it, which you do not have. I assume this is the issue you need to solve. The key to solving it is simply making sure that your loop stops at some point. How do you do that?

True is a boolean value. a while loop runs again and again for as long as the specified condition remains true, and in your case - it is always true. What you need to do is replace while(true) with while(some condition that will be false at some point). In your specific case, while(start <= end) seems appropriate - just make sure to increment start at every iteration.

Another way of doing it would be using a for loop, i.e:

for (int i = start; i <= end; i++) {
   // your code
}

This type of loop takes care of stating the conditions for which the loop should keep on running, and also takes care of incrementing the index i to make sure the loop stops running at some point.

Yaron Grushka
  • 235
  • 2
  • 10
0

I will make some small changes in your code only so that you will be able to understand quickly.

public static void divideByThree(int start, int end) {

    while(start! =end) {
        if (start % 3 == 0) {
        System.out.println(start);
            start++;
        }
} 
}