简体   繁体   中英

Reading text File and putting it in a array

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); }

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;

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM