Read Data From Text File And Sum Numbers

前端 未结 8 1491
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 13:59

I want to read in data from a text file which is full of integers and have the program print those integers out to the screen while summing them. This shouldn\'t be hard, b

8条回答
  •  轮回少年
    2020-12-21 14:28

    You are getting the error (exception) because your file has very big numbers that do not fit on Integer types. You should use Long.

    So, use: aScanner.nextLong() instead of aScanner.nextInt().

    EDIT: You should also change the type of the sum variable from int to long.

    Your code will work fine.

提交回复
热议问题