3 Replies - 507 Views - Last Post: 05 October 2012 - 11:55 AM Rate Topic: -----

#1 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Error: Void type not allowed while printing array

Posted 05 October 2012 - 11:23 AM

Hello All

I'm trying to work on a random number generator where the user can determine the amount of numbers. This piece below has had me stumped for the last few hours.... It is not the complete program, but I can provide that if needed.

I keep receiving the "error:'void' type not allowed here message on line 02 when I try access my 'printArray' method and I'm not sure why. Any nods in right direction would be greatly appreciated. This is only the 4th time I have had to write a program!

listSize = input.nextInt();
    	printArray(generate_data(listSize));
    }
    }
    public static void generate_data(int size){
    	int [] randomArray = new int [size];

    	for (int i=0; i<randomArray.length; i++){
    		randomArray[i] = 1+(int)(Math.random()*9999);
    	}
    }
    public static void printArray(int[]s){
		for(int i = 0; i<s.length; i++){
		System.out.print(s[i] + " ");
		}
	}
  


Is This A Good Question/Topic? 0
  • +

Replies To: Error: Void type not allowed while printing array

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Error: Void type not allowed while printing array

Posted 05 October 2012 - 11:29 AM

your printArray method is receiving a void argument. This is because your generate_data (This name bothers me. Naming methods should always have the first word uncaps, every word after with caps EX: generateData / generate_Data) returns a void, but your printArray takes a int[]. Just change your generate_data to something like this.

    public static int[] generate_data(int size){
    	int [] randomArray = new int [size];

    	for (int i=0; i<randomArray.length; i++){
    		randomArray[i] = 1+(int)(Math.random()*9999);
    	}
        return randomArray;

    }


Was This Post Helpful? 1
  • +
  • -

#3 mike31s  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 05-October 12

Re: Error: Void type not allowed while printing array

Posted 05 October 2012 - 11:46 AM

That fixes it. Its always the small details that are the hardest!
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Error: Void type not allowed while printing array

Posted 05 October 2012 - 11:55 AM

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1