0

I'm new to coding and I'm required to have a return statement but I'm struggling on how to return the values of count from my while loop. I put a link of picture of what I'm getting plus my code is below.

Also I added a printout of "is factor of number " + count just so I can see what count is doing during the while loop. What I really want to do is return these values so when I hit run these values and only these values appear.

Here is my code:

package allFactors;

public class AllFactors {

    public static void main(String[] args) {
        System.out.println(printFactors(25));
    }

    public static int printFactors(int number) {
        if (number < 1) {
            System.out.println("Invalid Value ");
        }

        int count = 0;
        while (count <= number) {
            count++;

            if (number % count == 0) {
                System.out.println(count + " is factor of number ");
                continue;
            }
        }
        return count;
    }
}

my CURRENT output is as follows:

1 is factor of number

5 is factor of number

25 is factor of number

26

  • i only put system.out.println(count + " is factor of number "); BECAUSE i want to see what my count value is, NOW i dont want to return count; because that just gives me 26, i want to JUST return 1, 5 and 25. Because these numbers are all factors of 25; once i can do this i will delete my system.out.println(count + " is factor of number "); line of code because it is not necessary. I hope this question make sense

DESIRED OUTPUT

1

5

25

4
  • 1
    please add your current and desired output. Commented Jun 18, 2020 at 17:23
  • my current output is: 1 is factor of number 5 is factor of number 25 is factor of number 26 desired output is: 1 5 25 Commented Jun 18, 2020 at 18:10
  • for some reason i cannot type or copy paste my output in more understandable and realistic view, i added outcome in original question and it is pretty close to what i see on screen, i hope you can see it there and it helps. Commented Jun 18, 2020 at 18:16
  • A function in java can return only one value/object. If you want to return multiple values from a function, you must create a Collection of all the values and return that. Commented Jun 22, 2020 at 17:02

4 Answers 4

1

Here you want to return all the factors of the number : So you can use the list to store all the factors and then return that list and print it, code is this -

    package allFactors;

    public class AllFactors {
       public static void main(String[] args) {
           printFactors(32).forEach(System.out::println);
       }

       public static List<Integer> printFactors(int number) {
           if (number < 1) {
                System.out.println("Invalid Value ");
           }
           List<Integer> all_factors = new ArrayList<>();
           int count = 0;
           while (count <= number) {
               count++;
               if (number % count == 0) {
                   all_factors.add(count);
                   System.out.println(count + " is factor of number ");
                   continue;
               }
           }
          return all_factors;
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

return is like the output of your function. In the code you said return 0, so no matter what, it will only print 0. Change it to return count; is how you do it.

Comments

0

just return the count from the end of the method as follows

public static int printFactors(int number) {
    if (number < 1) {
        System.out.println("Invalid Value ");
    }

    int count = 0;
    while (count <= number) {
        count++;

        if (number % count == 0) {
            System.out.println(count + " is factor of number ");
            continue;
        }
    }
    return count;
}

1 Comment

please look back at my question i just updated it, thanks for trying to help!
0

if you want to return all values you need to use some collection please check below code

package allFactors;

import java.util.ArrayList;
import java.util.List;

public class AllFactors {

public static void main(String[] args) {
    System.out.println(printFactors(32));
}

public static List<Integer> printFactors(int number) {
    if (number < 1) {
        System.out.println("Invalid Value ");
    }
    List<Integer> factors = new ArrayList<>();
    int count = 0;
    while (count <= number) {
        count++;

        if (number % count == 0) {
            factors.add(count);
            System.out.println(count + " is factor of number ");
        }
    }
    return factors;
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.