1

So I'm trying to pass the variable numbers into the printArray method but I'm not completely sure how to do it. Obviously I'm not doing it right but could someone please point me in the correct direction? Thanks in advance.

import java.io.*;
import java.util.Scanner;
public class Paniagua_ArrayProcessing{
    public static void main(String[] args) throws IOException{
            int[] numbers = new int[4];
            inputData();
            printArray(numbers);
    }
    public static int[] inputData() throws IOException{
            Scanner keyboard = new Scanner(System.in);
            String input;
            int lines;
            System.out.println("Please enter a file name: ");
            input = keyboard.nextLine();
            File myfile = new File(input);
            if (!myfile.exists()){
                    System.out.println("File not found.");
                    System.exit(0);
            }
            Scanner inputFile = new Scanner(myfile);
            lines = inputFile.nextInt();
            int[] numbers = new int[lines];
            for (int i=0; i<lines; i++){
                    if (inputFile.hasNextInt()){
                            int moreLines = inputFile.nextInt();
                            numbers[i] = moreLines;
                    }
            }
            inputFile.close();
            return numbers;
    }
        public static void printArray(int[] array){
                System.out.println(array[3]);
    }

}
2
  • Dont exit with 0 in the event of a program failure; by convention, exit code 0 is used to indicate success Commented Mar 15, 2014 at 23:55
  • Note that this being Java, you should avoid returning 1 as well, since this is the return code of the JVM when main() throws an exception Commented Mar 15, 2014 at 23:57

2 Answers 2

2

instead of

 int[] numbers = new int[4];
 inputData();

just

int[] numbers =  inputData();

you are creating an array then you are creating again in inputData() and filling data in it and returning reference value so first initialization is useless here

Sign up to request clarification or add additional context in comments.

Comments

0

I am not clear, on what you want you are doing here. I assume that you want to read all the numbers from the file and print the "Whole" Array.

Your Integer Array Variable "numbers" has not been initialized with the values read from the file and hence, you will not see any output If you run this code. To Fix, this, you need to assign the int[] variable returned from the method call inputData() to "numbers" Variable. I have updated these changes and tested the code on a sample file containing data 1 2 3 4 5 This worked fine for me. Hope this helps you.

Note - Numbers in the Main method and inputData() Methods are different.

package test;

import java.io.*;
import java.util.Scanner;
public class test{
    public static void main(String[] args) throws IOException{
        int[] numbers = inputData();
        printArray(numbers);
    }
    public static int[] inputData() throws IOException{
        Scanner keyboard = new Scanner(System.in);
        String input;
        int lines;
        System.out.println("Please enter a file name: ");
        input = keyboard.nextLine();
        File myfile = new File(input);
        if (!myfile.exists()){
            System.out.println("File not found.");
            System.exit(0);
        }
        Scanner inputFile = new Scanner(myfile);
        lines = inputFile.nextInt();

        //Find the Number of Lines
        int count = 0;
        while (inputFile.hasNextLine()){
                    count++;
                    inputFile.nextLine();
                }
        int[] numbers = new int[count];
        inputFile.close();
        inputFile = new Scanner(myfile);
        for (int i=0; i<count; i++){
            if (inputFile.hasNextInt()){
                numbers[i] =  inputFile.nextInt();
            }
        }
        inputFile.close();
        keyboard.close();
        return numbers;      
    }
    public static void printArray(int[] array){
        for(int i=0;i<array.length;i++) {
        System.out.println(array[i]);
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.