-1

I am trying to write a program that will take the integer input from a user and print out its factors. I'm trying to do this by creating a class FactorGenerator and methods nextFactor and hasMoreFactors.

The nextFactor method must return an integer and the hasMoreFactors must return a boolean.

I am a bit confused as to how to declare a method that returns a boolean. Because, I think that if I'm able to do that properly, most of my errors might go away. Please guide me as to what I need to fix because I've spent a lot of time trying to fix the code and look up boolean methods but haven't reached anywhere.

import java.util.Scanner;
import java.lang.Boolean;
public class myProgram 
{

    public static void main(String args[]) 
    {
      Scanner in = new Scanner(System.in);
      System.out.print("Enter an integer: ");
      int num = in.nextInt();
      FactorGenerator myFacGenerator = new FactorGenerator(num);
      while (myFacGenerator.hasMoreFactors())
            System.out.println(myFacGenerator.nextFactor);
            

    
    }

    public int FactorGenerator(int number)
    {
           int number = num1;
           return num1;
            
    }
    
    public int nextFactor(int num1) // method that returns the factors of the input number 
    {
    
    int i;
    System.out.format("Factors of %d\n " + num1);
        
        for (i = 1; i <= num1; i++) 
        {
            
            if (num1 % i == 0) 
            {
                
               return i;
            
                
            }
        }
    }
    
    public Boolean hasMoreFactors(boolean b) // method that is meant to check if there are any more factors left. This method returns boolean 
     {
       Boolean b1 = num1.equals(i);
       
       return b1;
    
     }
}

` Some of the error messages include:

MyProgram.java:27: error: illegal start of expression
     public int nextFactor(int num1) //Returns integer
     ^
MyProgram.java:27: error: ';' expected
     public int nextFactor(int num1) //Returns integer
                          ^
MyProgram.java:27: error: ';' expected
     public int nextFactor(int num1) //Returns integer
                                   ^
MyProgram.java:45: error: illegal start of expression
    public Boolean hasMoreFactors(boolean b); // Returns boolean
    ^
MyProgram.java:45: error: ';' expected
    public Boolean hasMoreFactors(boolean b); // Returns boolean
                                 ^
MyProgram.java:45: error: ';' expected
    public Boolean hasMoreFactors(boolean b); // Returns boolean
                                           ^
6 errors
munchy
  • 1
  • 1

1 Answers1

0

Try the following :

    public int nextFactor(int num){
        for(int i = 2; i <= num; i++){
            if(i == num){
                return 1; // returns 1 if its a prime number
            }else (num % i == 0) { 
                return 1; // returns lowest factor
            }
        }
        return 0; // return 0 if all paths fail
    }
    
    public boolean hasMoreFactors(int num){
        return (nextFactor(num) > 1); //if greater than 1(Not a prime), it has more factors
    }

Zach_Mose
  • 357
  • 3
  • 7