1

I've been running into the issue of calling the class that's supposed to print even and odd numbers into the main method, printing each number without overwriting the previous one. I've tried replacing the public int with public void, and it still didn't work, if you guys could help me it would help a lot. this is the code:

package myprojects;

import java.util.*;

class ArrayMethod {
    private int[] Array;

    public void Calc(int[] Array) {
        this.Array = Array;
    }

    public int Sum() {
        int sum = 0;
        for (int i = 0; i < Array.length; i++) {
            sum += Array[i];
        }
        return sum;
    }

    public double Average() {
        return Sum() / Array.length;
    }

    public int PrintOdd() {
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 != 0) {
                int Odd = Array[i];

                System.out.println(Odd);
            }
    }

    public int PrintEven() {
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 == 0) {
                int Even = Array[i];
                System.out.println(Even);
            }
    }
}
public class ArrayEX {

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

        ArrayMethod B = new ArrayMethod();
        int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        B.Calc(A);
        System.out.println("Sum of the numbers inside array is " + B.Sum());
        System.out.println("Average of the numbers inside array is " + B.Average());
        System.out.println("Even numbers " + B.PrintEven());
        System.out.println("Odd Numebrs " + B.PrintOdd());


    }
}
5
  • What is the problem exactly? The compiler should currently tell you that int PrintEven() for example needs to return something but it doesn't. You also try to evaluate (print) the returned result of those methods in "Even numbers " + B.PrintEven() for example. What number(?) should it print in those lines? Commented Oct 10, 2022 at 22:35
  • Okay, what is exactly your question? And please take a look at java code conventions Commented Oct 10, 2022 at 22:35
  • @zapl hey there, that's my current issue aswell, where do I insert return in the line of code? when I'm inserting it outside it tells me that 'variable' is undefined. Commented Oct 10, 2022 at 23:00
  • I don't know what variable is either and I don't know what you wanted to return from those methods. If they don't need to return a result value (like your Sum method) , make them void and maybe do System.out.println("Even numbers"); B.PrintEven();? Commented Oct 10, 2022 at 23:11
  • You are probably new to Java too. As any other programming language, Java is used also to communicate your algorithm to other fellow programmers. Hence, it is very important to use the code conventions. JusT THInk how AnnOYINg it is tO rEAd soMEthInG LIKE tHIs. Commented Oct 15, 2022 at 10:11

2 Answers 2

2

thank you to everyone who answered my question, the issue was resolved by making PrintOdd and PrintEven methods into Void type and calling it to main method outside of a print System.

my dearest thanks to @popovko57 and @zapl, who provided the solution.

Sign up to request clarification or add additional context in comments.

1 Comment

This is more of a thank-you than an answer. Maybe it would be better to move it to a comment on a referred answer. If you like to provide an answer, then please submit a full explanation and a code example.
1

If your function return nothing, you need to use void type method.

First you can update PrindOdd and PrintEven method to print each odd and even numbers when these functions are called:

class ArrayMethod {
    
    ...

    public void PrintOdd() {
        System.out.println("PrintOdd");
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 != 0) {
                int Odd = Array[i];

                System.out.print(Odd + " ");
            }
    }

    public void PrintEven() {
        System.out.println("PrintEven");
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 == 0) {
                int Even = Array[i];
                System.out.print(Even + " ");
            }
        System.out.println();
    }
}

To print the result you need to update the way to call these functions:

public class Main {

    public static void main(String[] args) {
        
        ....

        B.PrintEven();
        B.PrintOdd();

    }
}

I hope that answers your question

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.