1

I am calling the void method

printDivisors(inputNum)

and getting this error:

'void' type not allowed here

I concatenate the call to the method in a string and the compiler keeps complaining...

Here is the call to a method in a code:

if (determinePrime(inputNum)) {
    JOptionPane.showMessageDialog(null, "Prime");
} else {
    JOptionPane.showMessageDialog(null, "Not Prime" + printDivisors(inputNum));
}

Below you can see the full program:

import javax.swing.JOptionPane;
    public class method_01 {
    public static void main (String [] args) {
        String input;
        int inputNum;

        // Determine Prime

        do {
            input = JOptionPane.showInputDialog("Enter a positive number please");
            inputNum = Integer.parseInt(input);
        } while (inputNum < 1 );

        if (determinePrime(inputNum)) {
            JOptionPane.showMessageDialog(null, "Prime");
        } else {
            JOptionPane.showMessageDialog(null, "Not Prime" + printDivisors(inputNum));
        }
    }

    // Determine Prime Method

    public static boolean determinePrime(int num) {

        for (int i = 2; i < num ; i++) {
            if (num % i == 0) 
                return false;
        }
        return true;
    }

    // Print all the divisors of a number

    public static void printDivisors(int number) {

        for (int i = 1; i <= number; i++ ) {

            if (number % i == 0)

                System.out.print("\t" + i); 
        }
    }
}

Any help would be greatly appreciated.

1
  • Could you post the complete error message?
    – Blip
    Commented Apr 30, 2015 at 4:26

2 Answers 2

3

Your declaration is void here:

public static void printDivisors(int number)

If you are using it inside the function, you should return a string instead. i.e. write to a string instead of

System.out.print("\t" + i);

and use + to append the string at the end of your code:

JOptionPane.showMessageDialog(null, "Not Prime" + printDivisors(inputNum));}
2
  • public static String printDivisors(int number)
    – Ivan Ling
    Commented Apr 30, 2015 at 4:20
  • Thank you so much Ivan for your quick response. I am new to java that is why ran into this issue... Thank you for clarification!
    – Vi Pshnchk
    Commented Apr 30, 2015 at 4:29
-1

The problem is in

JOptionPane.showMessageDialog(null, "Not Prime" + printDivisors(inputNum));

I suppose you are trying to print the divisor, but it returns null so ideally, when the compiler compiles, it searches for a value, as you have " + " sign after "Not Prime", but does not get anything. thus it is throwing an exception.

either change the return type of printDivisors method or remove that part from the parameter.

3
  • 3
    A method that doesn't return anything is different than a method that returns null.
    – Makoto
    Commented Apr 30, 2015 at 4:18
  • Hi Saurabh! Thank you for your quick response. I am new to Java that is why might not understand why that happened. Just to clarify for myself, printDivisors method is a void method, I thought it doesn't return anything and just prints..
    – Vi Pshnchk
    Commented Apr 30, 2015 at 4:21
  • you are right it does not return anything, but when you are using it in JOptionPane.showMessageDialog(null, "Not Prime" + printDivisors(inputNum)); the copiler is expecting some return value from the method Commented Apr 30, 2015 at 4:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.