asdf
asdf

Reputation: 37

how do i use an array i returned in my other method in java?

This is the array in the method that I'm returning:

public static int[] inputData()
{
Scanner kb = new Scanner(System.in);
System.out.println("Please enter a file name: ");
String inputFile = kb.nextLine();
File file = new File(inputFile);
Scanner read = new Scanner(file);
if(!file.exists())
  {
  System.out.println("Error. File not found. Program will now exit.");
  System.exit(0);
  }
int j = read.nextInt();
int[] input = new int[j];
for(int i = 0; i < input.length; i++)
  {
  input[i] = read.nextInt();
  }
return input[i];
}

And this is my main method

  public static void main(String[] args) throws IOException
 {

int[] input = new inputData();
printArray(input);
reverseArray(input);
sum(input);
mean(input);
min(input);
max(input);
evenOdd(input);
}

How do I use the array i returned in the main method?

Upvotes: 0

Views: 53

Answers (3)

NDY
NDY

Reputation: 3557

There are two things you need to fix in your code.

Make the method return the array:

return input;

And your inputData is a method, so you dont need to use new. Change it to this.

int[] input = inputData();

Upvotes: 1

Jason
Jason

Reputation: 11832

This line in your code is wrong:

int[] input = new inputData();

This infers that inputData is a class. It should be:

int[] input = inputData();

And the inputData method should return the entire array, not just one element.

Upvotes: 1

First: make inputData actually return the array:

return input;

Second: like this:

int[] input = inputData();

Upvotes: 1

Related Questions