0

I am trying to read a series of integers from a file into an ArrayList but when accessing numbers.get(0), I get the Out of Bounds Exception, presumably because nothing has been written to the list.

ArrayList<Integer> numbers = new ArrayList<Integer>();

public void Numbers() throws IOException{

  File file = new File("Numbers.txt");
  Scanner inputFile = new Scanner(file);

  while (inputFile.hasNext()){

    numbers.add(inputFile.nextInt());
  }

    inputFile.close();
}

Any help would be greatly appreciated. I can provide more code snippets if needed.

4
  • 2
    I don't see a line of code that says numbers.get(0). Where is this code? Commented Nov 29, 2012 at 0:36
  • Is the file actually being opened? Commented Nov 29, 2012 at 0:37
  • 1
    The first token in the file can't be interpreted as an integer. Commented Nov 29, 2012 at 0:40
  • I call it in another method like: int lowest = numbers.get(0); Commented Nov 29, 2012 at 0:42

2 Answers 2

4

One likely problem is that you have declared the method as

public void Numbers() throws IOException

This is a method called Numbers which returns void and throws IOException. Note that this is not a constructor, which you might intend it to be, because you have declared a return type. If you are calling numbers.get(0) in another method of this same class. This Numbers() method is probably not called explicitly if you expect it to be called automatically as a constructor.

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

4 Comments

I did intend this to be the constructor so it sounds like this is the problem. Now that I have removed the return type I get the error, Error: unreported exception java.io.IOException; must be caught or declared to be thrown.
Btw, this is the line im getting the error on: public static void main(String[] args){ Numbers n = new Numbers();
@Randallsm83 You need to do exactly what the error message says: either catch the IOException which your constructor throws or declare main() to throw it.
Thank you, I haven't learned that just yet but I am going to right now!
1

I think its trying to read the token as int and comes out with exception. Try this:

try{
   File file = new File("Numbers.txt");
   Scanner inputFile = new Scanner(file);
   while (inputFile.hasNext()){
    String next = inputFile.next();
    try{
        numbers.add(Integer.valueOf(next));
     }catch(NumberFormatException nfe){
       //not a number, ignore it
     }
   }
 }catch(IOException ioe){
      ioe.printStackTrace();
 }

6 Comments

After fixing the constructor format I get the error unreported exception java.io.IOException; must be caught or declared to be thrown as stated above. I have not learned how to catch yet unfortunately. Is this what I need to learn to alleviate this error?
Consider using hasNextInt() and nextInt().
@Randallsm83 Updated the answer with IOException handling. Please put the code inside your constructor and remove the throws IOException from declaration.
@Randallsm83: In addition, please make sure you perform a size check before int lowest = numbers.get(0); otherwise you will again get IndexOutOfBoundException if your constructor couldn't read your file and numbers list wasn't populated.
Perfect! I know the problem now is my book didn't touch on catching the exception which is exactly what it need. Thank you everyone so much for the quick help!
|

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.