添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm attempting to read a certain file in Java and make it into a multidimensional array. Whenever I read a line of code from the script, The console says:

Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

I know that this error is caused when the coding can't reach the specific index, but I have no idea how to fix it at the moment.

Here is an example of my coding.

int x = 1;
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  //Explode string line
  String[] Guild = line.split("\\|");
  //Add that value to the guilds array
  for (int i = 0; i < Guild.length; i++) {
    ((ArrayList)guildsArray.get(x)).add(Guild[i]);
    if(sender.getName().equals(Guild[1])) {
      //The person is the owner of Guild[0]
      ownerOfGuild = Guild[0];

**Text Document **

Test|baseman101|baseman101|0|
Test2|Player2|Player2|0|

Other solutions, such as the one found here: Write to text file without overwriting in Java

Thanks in advance.

Well, Exception says what's happening at Exception level. Array is size 1 and you access position 1. Your logic is not ok! – diegoaguilar Oct 19, 2013 at 4:00 They do increase dinamically, however they're 0 based and accessing to a bigger index than the actual one will result in an Exception – diegoaguilar Oct 19, 2013 at 5:02

You are increasing x so if x >= guildsArray.size() then you will get java.lang.IndexOutOfBoundsException

solution

if( x >= guildsArray.size())
      guildsArray.add(new ArrayList());
for (int i = 0; i < Guild.length; i++) {
    ((ArrayList)guildsArray.get(x)).add(Guild[i]);
    if(sender.getName().equals(Guild[1])) {
      //The person is the owner of Guild[0]
      ownerOfGuild = Guild[0];
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.