Strophs
Strophs

Reputation: 15

When compiling program I get the error 'void' type not allowed here

I'm trying to create a program that asks for 10 integers and puts those numbers into an array of negative, positive, and odd arrays. In the end, I want the program to print out 3 rows of numbers that separate the users 10 numbers into "odd", "even", and negative". When I run this I get "error: 'void' type not allowed here"

import java.util.Scanner;

public class ArrayPractice{

   private static void showArray(int[] nums)
   {
      for (int i=0; i<nums.length;i++)
      {
         if(nums[i]!=0)
         {
            System.out.println(nums[i] + " ");
         }
      }
   }

   public static void main(String[] args){
      int evenArray[] = new int[10];
      int evenCount = 0;
      int oddArray[] = new int[10];
      int oddCount = 0;
      int negArray[] = new int[10];
      int negCount = 0;
      Scanner input = new Scanner(System.in);
      for(int i = 0; i<10; i++)
      {
         System.out.println("Number? " + (i + 1));
         int answer = input.nextInt();
         if(answer<0)
         {
            negArray[negCount++] = answer;
         }
         else
         {
            if (answer % 2 == 0)
            {
               evenArray[evenCount++] = answer;
            }
            else
            {
               oddArray[oddCount++] = answer;
            }
         }
      }
      System.out.println(showArray(evenArray));       
      System.out.println(showArray(oddArray));
      System.out.println(showArray(negArray));

   }
}

Upvotes: 0

Views: 37

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

showArray is void, it does not return anything. And, on inspection, it prints in the method itself. So this

System.out.println(showArray(evenArray));       
System.out.println(showArray(oddArray));
System.out.println(showArray(negArray));

should just be

showArray(evenArray);       
showArray(oddArray);
showArray(negArray);

Upvotes: 2

Related Questions