-3

Whenever I run this code, I keep getting an ArrayOutOfBoundException. I don't know whats wrong, but after looking through it, the error is when I try to convert the string to a number and set it into an array. Any ideas on why and how to fix it?

Sum.txt:

12 2 3 4 5 6 7 8 9 10

90183284 132947132984

1341324245 45 2435 2345 32 6 7  7 578

code:

import java.io.*;
import java.util.*;

public class Sum {
static int lines = 0;
static int arrayvalue = 0;

public static void main(String[] args) {

    System.out.println(addnumbers(readFile()));
}

public static int[] readFile() {
    String nextline;
    int[] inputinarray = new int[25];

    try {
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        Scanner input = new Scanner(new File("sum.txt"));

        while (input.hasNext()) {
            nextline = input.nextLine();
            for (int i = 0; i < inputinarray.length; i++) {
                // inputinarray populated as integer array
                // ***The error happens here:****
                inputinarray[i] = Integer.parseInt(nextline.split(" ")[i]);
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("File not found");

    }
    return inputinarray;
}

public static int addnumbers(int[] inputinarray) {
    int rowtotal = 0;
    for (int i = 0; i < inputinarray.length; i++) {
        rowtotal += inputinarray[i];
    }
    return rowtotal;

}
Jamie
  • 57
  • 1
  • 2
  • 10
  • 1
    Hint. Debug this line : `inputinarray[i] = Integer.parseInt(nextline.split(" ")[i]);` – Suresh Atta Nov 27 '15 at 18:49
  • `nextline.split(" ")[i]` - how do you know that the split result will have at least that many elements? –  Nov 27 '15 at 18:49
  • 2
    print this `nextline.split(" ").length`, the value will be less than 25. – YoungHobbit Nov 27 '15 at 18:50
  • 1
    Please add the contents of the sum.txt. – burna Nov 27 '15 at 18:51
  • 1
    You need to rethink how you're going to store each line. Currently you're overwriting your input array on each line. – azurefrog Nov 27 '15 at 18:53
  • in short: don't use any input arrays here, use `for( String s : line.split(" " ) )` to iterate over the split results, with `sum += Integer.parseInt(s);` as the iteration step. –  Nov 27 '15 at 18:58
  • Your `i` is limited by `inputinarray.length()`. Are you sure that this limit is equal or less than number of elements returned by `nextline.split(" ")` since you are using `nextline.split(" ")[i]`? – Pshemo Nov 27 '15 at 19:14

1 Answers1

0

You implicitly assume that every line of your sum.txt file has at least i elements (i up to 25), separated by " ". If this is not true, there will be the stated Exception.

If you want to sum up all numbers in the file, you have to have nested loops. You loop through the lines, then split the line and then loop through the entries of the split array inside.

trincot
  • 317,000
  • 35
  • 244
  • 286
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • 2
    although what you state is true, you still hasn't answered **how to fix it?** –  Nov 27 '15 at 19:00