Question

I'm having some difficulties figuring out exactly how tot initialize my arrays for a project. I have an array being created in one method and it being called and copied in another then called to another method to be printed and i can't seem to get it to work correctly.

import java.util.*;
import java.io.*;
public class Tention_ArrayProcessing{ 
public static void main(String[] args) throws IOException{
        int[] inputData = inputData();
        int[] array = new int[SIZE];
        for (int i =0; i < inputData.length; i++)
        array[i] = inputData[i];
        printArray();
        }

public static int[] inputData() throws IOException{
// gets file name   
        String data;
        Scanner filename = new Scanner(System.in);
        System.out.println("Enter a file name please. Include extension");
        data = filename.next();
        filename.close();
//Reads from file and places data in array
        int i = 0;
        File file = new File (data);
        Scanner inputFile = new Scanner(file);
        final int SIZE = inputFile.nextInt;
        int[] inputData = new int[SIZE];
        while (inputFile.hasNext() && i < inputData.length){
        inputData[i] = inputFile.nextInt();
        i++;
        }
        System.out.println();
        inputFile.close();
        return inputData;
        }
public static void printArray(int[] array){
//calls inputData method for array then prints it.
        int c = 0;
        System.out.println("Printing Array:");
        for( int i = 0; i < 100; i++)
        System.out.print(array[i] +" ");
        c++;
        if (c == 10){
        System.out.println();
        }
        }
        }
Was it helpful?

Solution

int[] array = new int[inputData.length];

printArray(array);

int SIZE = inputeFile.nextInt();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top