0

i am new to java, i have tried to read a file and saved into Hashmap. But it showing ArrayIndexBounds Of Exception.i dont know how to solve it.

here is my code,

import java.io.BufferedReader;  
import java.io.FileReader;  
import java.util.HashMap;  
import java.util.Map;


public class excer4 {

        public static void main(String[] args) throws Exception {
            Map<String, String> map = new HashMap<String, String>();
    BufferedReader in = new BufferedReader(new  FileReader("/home/mansoor/Documents/exm.txt"));
            String line = "";
            while ((line = in.readLine()) != null) {
                String parts[] = line.split("\t");
                map.put(parts[0], parts[1]);
            }
            in.close();
            System.out.println(map.toString());
        }
    }

my input is:

“u1” “u10”
“u2” “u41”
“u3” “u10”
“u4” “u81”
“u5” “u10”
“u6” “u10”
“u7” “u31”
“u8” “u11”

Output i got :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at excer4.main(excer4.java:15)

can any one help me to find a solution..

1
  • Check for a blank line on the end of your file -- almost guaranteed that at some point there isn't a tab, so there's no second part to put into the map. Commented Aug 4, 2014 at 9:04

4 Answers 4

1

When you split:

String parts[] = line.split("\t");

Before you try to access parts[0] and parts[1], you should check if they exist.

Try to debug your code to understand why are you getting this error. The debugger will help you to really understand what's going on.

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

Comments

1

Solution: enter tab seperated data and make sure there there is no blank line at the end of your inputfile. Your error indicates that the split does not return an array with 2 entries. I would recommend debugging your application to see where this error occurs as it's a handy way to learn new things about Java.

And are you sure you want \t which is for tabs, and not \s which is for spaces. As your sample input seems to be space seperated

Comments

1

String parts[] = line.split("\t")

The split does not return you with only one element in case there is no "\t" and hence it throws an ArrayOutOfBoundsException for the second element - parts[1].

Things you can do :

  1. Check for a new line or a space at the end of file which is not a part of your tab separated data.
  2. Before splitting , check for the index of "\t" and then proceed with the split.

Comments

0

Most likely your input file contains an empty line (the last one?) which when splitted results in an empty array, thus referring to its first element (parts[0]) results in an ArrayIndexOutOfBoundsException.

Check if the parts array has at least 2 elements and only then proceed to put it into the map:

if (parts.length > 1)
    map.put(parts[0], parts[1]);

Comments

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.