1

I am trying to take a set of 25 numbers from a text file and convert it into a array. But I am lost.

I have read some other questions similar to this, but all of them used imports and extras, and I don't want to use any imports besides import java.io.*; nor any list. Also the for loop within this is method is me just messing with it, because I couldn't figure it out.

public static int[] processFile (String filename) throws IOException, FileNotFoundException {
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;
    int[] a = new int[25];
    while (( line = inputReader.readLine()) != null){
        int intValue = Integer.parseInt(line); //converts string into int
        for (int i = 0; i < a.length; i++){
            a[intValue]++;
        }

    }
    return a;
}
 public static void printArray (int[] a) {
    for (int i = 0; i<a.length; i++) {
    System.out.println (a[i]);
    }

} public static void main(String[] args) throws IOException, FileNotFoundException { int [] array = processFile("C:\Users\griff_000\Desktop\TestWeek13.txt"); printArray(array); }

2
  • 1
    Can you give us an example of the text file you're reading from? Commented Apr 26, 2015 at 17:34
  • Each on a separate line 5 14 3 80 7 45 14 90 66 45 13 32 9 10 54 2 76 5 89 43 6 16 54 63 79 Commented Apr 26, 2015 at 17:49

3 Answers 3

0

I'm unclear about your whole import restriction, why exactly are you trying to limit the number of imports you have?

Anyway, looking at your code, it seems like the concept of arrays isn't all that clear with you.

Arrays are accessed under the syntax:

array[index] = value;

looking at your code, the line a[intValue]++; is actually finding the array index intValue (the number read from file) and incrementing it by one. Not only is this not what you want, numbers over the array length will cause an ArrayIndexOutOfBoundsException.

Making said amendments we get:

public static int[] processFile (String filename) throws IOException, FileNotFoundException{
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;

    int[] a = new int[25];

    int i = 0; // We need to maintain our own iterator when using a while loop

    while((line = inputReader.readLine()) != null){
        int intValue = Integer.parseInt(line); //converts string into int

        a[i] = intValue; // Store intValue into the array at index i

        i++; // Increment i
    }
    return a;
}

note the additional variable i being used in this context to facilitate the incrementing index number being used to access the array. If you examine this method carefully, a input file longer than 25 elements would also throw ArrayIndexOutOfBoundsException due to the variable i becoming 25 (beyond the limits of the array). To fix, I'd suggest changing the loop structure to a for-loop (assuming your input array is of fixed size) as follows:

public static int[] processFile (String filename) throws IOException, FileNotFoundException{
    BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
    String line;

    int[] a = new int[25];

    for(int i = 0; i < a.length; i++){
        String line = inputReader.readLine(); // Move the readline code inside the loop

        if(line == null){
            // We hit EOF before we read 25 numbers, deal appropriately
        }else{
            a[i] = Integer.parseInt(line); 
        }
    }

    return a;
}

Note how the for loop integrates the iterator variable into one nice elegant line, keeping the rest of the code neat and readable.

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

1 Comment

Okay thanks so much! Also the array[index] = value; helped me think of it a different way, good deal :).
0

Your mistake is in the line a[intValue]++;. You are telling Java to find the element at [intValue] and add 1 to it's current value. From your question, I understood that you want to put intValue as the array element.

Since you are using i as the iterator, to add the element simply use:

a[i] = intValue;

1 Comment

I tried that and it only outputs my last number in my text file (79). My other methods to print it are now in question. Could that be it?
0

What you are doing here:

a[intValue]++;

is increasing the array position of the read value by one. If the number read is 2000 you are increasing a[2000]

you might want to do this

a[i]=intValue;

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.