1 Replies - 44275 Views - Last Post: 30 November 2006 - 09:03 AM Rate Topic: -----

#1 Thrawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-November 06

returning arrays from methods

Posted 30 November 2006 - 08:36 AM

Hello, ive been given a task to get a user to enter a array length, and the fill the array i then have to find the average, smallest and largest of these numbers, if someone would be so kind as to tell me how to return an array so that i can process it, i would be most appreciative, thanks kev
import javax.swing.*;

class homework
{
  public static void main(String args[])
  {
	String lengthString = 
	JOptionPane.showInputDialog(
	  null, "Please enter the length of the mark array",
	  "Length", JOptionPane.QUESTION_MESSAGE);
	  
	  int length = Integer.parseInt(lengthString);
	  
	  int mark[] = new int [length];	  

	  System.out.println(getMark(mark,length));
  }
  public static int getMark(int mark[], int length)
  {
	int i = 0;
	
	String numberString = "";
	for(i = 0; i < length; i++)
	{
	  numberString = 
	  JOptionPane.showInputDialog(
		null, "Please enter your marks",
		"Marks", JOptionPane.QUESTION_MESSAGE);
		
		mark[i] = Integer.parseInt(numberString);
	}
	return mark[i];
  }
}


edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

#17 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: returning arrays from methods

Posted 30 November 2006 - 09:03 AM

if you want get mark to return an array, the return type must be of type array.
you currently have a single int: public static int getMark(int mark[], int length)
you want:
public static int[] getMark(int mark[], int length)

the set of [] tells java to expect an array of ints rather than 1 int.

*please put [code ][ /code] tags around all code in the future.
Was This Post Helpful? 1

Page 1 of 1