Why am I getting void not allowed if my code doesn’t return any void type? [closed]

I’m new to coding. I’ve spent a good couple of hours trying to debug this.

My program is supposed to ask the user for the highest number they want (up to 100) and then generate 10 random addition problems and the user has to check if it is correct, and then tell the user how many they got correct.

Here is my main function which puts it all together. I’m getting the error:

method GenerateArray in class ArrayGenerator cannot be applied to given types; ProblemGenerator Problems = new ProblemGenerator(randomnumbers.GenerateArray());

but the “GenerateArray” method in class ArrayGenerator returns an ArrayList , and the “GenerateProblems” method in class ProblemGenerator takes in an ArrayList as parameter. None of the functions besides Main return type void. I’m using https://repl.it as an IDE.

import java.util.ArrayList;
import java.util.Scanner;

class Test{

    //Main Function
    public static void main (String[] args){

        //Declare Array List, Scanners and Variables
        Scanner answer = new Scanner(System.in);
        ArrayList <Integer> List = new ArrayList<>();
        String name;
        int highestnum;
        int score;
        String result;

        //Display Instructions to user
        //Create instance of UserInput 
        UserInput Exam = new UserInput();
        //Get name and store it in String
        name = Exam.userName();
        //Get highest number and store it in highestnum
        highestnum = Exam.gethighestNum();
        
        //Generate Array of Random Numbers
        //Create instance of ArrayGenerator class
        ArrayGenerator randomnumbers = new ArrayGenerator(highestnum);

        //Create array of random numbers and assign it to List
        //Ask user to enter numbers in the array and check if their answer is correct
        //Create instance of ProblemGenerator class
        ProblemGenerator Problems = new ProblemGenerator(randomnumbers.GenerateArray());

        //Call Method to Generate Problems and assign correct answers to score
        score = Problems.GenerateProblems();

        //Display user score in a string
        System.out.println(Problems.result(score));

    }

}

This is my UserInput Class

import java.util.Scanner;

//Declare Class & Scanner
class UserInput{
    Scanner input = new Scanner(System.in);

    // userName function
    public String userName(){

        // Ask user to enter name and store it in variable
        System.out.println("Please enter your name");
        String name = input.nextLine();
        return name;
    }
    
    //gethighestNum function
    public int gethighestNum(){

        int userInput;

        // Keep asking the user until a valid number is entered
        while (true) {
            System.out.print("Enter a number (greater than 10 and less than 100): ");

            // Check if the input is an integer
            if (input.hasNextInt()) {
                userInput = input.nextInt();

                // Check if the number is within the desired range
                if (userInput > 10 && userInput < 100) {
                    System.out.println("You entered: " + userInput);
                    break; // Exit the loop since a valid number is entered
                } else {
                    System.out.println("Invalid input. Please enter a number greater than 10 and less than 100.");
                }
            } else {
                // Consume the invalid input to avoid an infinite loop
                System.out.println("Invalid input. Please enter a valid number.");
                input.next();
            }
        }

        // Close the scanner to prevent resource leaks
        input.close();
        return userInput;
    }
}

My ArrayGenerator class generates an ArrayList of 20 random numbers, taking in a parameter to set the highest number. It returns the ArrayList.

import java.util.ArrayList;
import java.util.Random;

class ArrayGenerator{

    //Create varibles and ArrayList and Random
    int highestnum;
    ArrayList <Integer> randomnumberArray = new ArrayList<>();
    Random random = new Random();

    //Constructor
    public ArrayGenerator(int highestnum){
        this.highestnum = highestnum;
    }

    //GenerateArray function
    public ArrayList<Integer> GenerateArray(int highestnum){

        //Loop to generate 20 random numbers
        for (int i = 0; i < 20; i++) {
            int randomInt = random.nextInt(highestnum);
            randomnumberArray.add(randomInt);
        }
        return randomnumberArray;
    }
}

My ProblemGenerator class keeps choosing two numbers from the array and then writes it out as an addition problem, and then checks if the users answers is correct, and counts the amount that were right.

import java.util.ArrayList;
import java.util.Scanner;

class ProblemGenerator
{
    //Declare ArrayList & Scanner & Variables
    ArrayList<Integer> List = new ArrayList<>();
    Scanner ans = new Scanner(System.in);
    int answer; //Variable to check if user answer is correct
    int k;     //Variable to count number of correct answers

    //Constructor
    public ProblemGenerator(ArrayList<Integer> List)
    {
        this.List = List;
    }

    // Method to Generate Problems and check Answers
    public int GenerateProblems(ArrayList<Integer> List)
    {

        //Run 10 times
        for (int j = 1; j < 11; j++)
        {

            // Print two number from the ArrayList and ask the user to add them together
            System.out.println(" " + List.get(j) + "+" + List.get(List.size() - j) + "= ?");

            //Take user answer
            answer = ans.nextInt();

            //Check if correct
            if (answer == (List.get(j) + List.get(List.size() - j)))
            {

                // If correct increment k
                k++;
            } //end k

        } // end j 

        //return k 
        return k;
    }

    public String result(int k)
    {
        String finalresult = "You answered" + k + "questions correctly ";
        return finalresult;
    }

}

I tried assigning the returned ArrayList to another ArrayList I create in the main function but still an error.

  • 2

    GenerateArray wants an int, you’re not passing anything.

    – 

  • 2

    There’s quite a lot of code there. Please trim it down to a minimal reproducible example (stackoverflow.com/help/minimal-reproducible-example). Also, please make sure to indent the code consistently – as it is, it’s very difficult to read.

    – 

  • Welcome to SO! To fix your problem, you need to pass the “highestnum” variable as a parameter when calling “GenerateArray” in your “main” method. ProblemGenerator Problems = new ProblemGenerator(randomnumbers.GenerateArray(highestnum));

    – 

  • The code showing should generate a different error, namely: “The method GenerateArray(int) in the type Main is not applicable for the arguments ()“. Where is the error occurring? Please post the error message and indicate the offending lines. Also, as others have mentioned, please strive to post only well-formatted code. Code formatting rules aren’t there to help make code “pretty” but rather to help make code much easier for others to understand. This can be very important when you’re asking others (us) to help you with a problem.

    – 




  • Also, please review the site’s tour and How to Ask links which can help you to improve this question and your future questions.

    – 

In fact, you have two compilation errors.

  1. Calling randomnumbers.GenerateArray() without an int as an argument. This is due to the fact, that the constructor of ArrayGenerator takes it instead and you don’t do anything with it internally. Maybe you could either remove it from the constructor and pass it to the GenerateArray method or you remove the parameter from the GenerateArray method and reuse the supplied value to the constructor. Either way might be ok.

  2. Two lines below that you call: problems.GenerateProblems() which also requires an argument. In this case, it is a List of integers.

Besides that, I would recommend you to:

  1. Install a proper IDE, e.g. JetBrains IntelliJ IDEA Community. It is free and checks ahead of compilation a lot of type-errors which would stop the compilation
  2. Use Javas naming conventions, such that others understand your code right away.

Leave a Comment