java.lang.ArrayIndexOutOfBoundsException: 1 String segmentation array out of bounds, the error is 1

ava.lang.ArrayIndexOutOfBoundsException: 1

When split() splits a string, a data out-of-bounds exception occurs, and the error is 1

In the process of learning the redis database today, I read the txt file, split each row of data, store it in the hashmap collection, and then write it to the redis database, but it is reported that the array is out of bounds, and I am sure that the data is not out of bounds.
code show as below:

FileReader file = new FileReader("data\\student.txt");
        BufferedReader br = new BufferedReader(file);
        String line;
        while ((line=br.readLine())!=null){
            String id ="student:"+ line.split(",")[0];

            String[] split = line.split(",");

            System.out.println(split.length);

            String name = split[1];
            String age = split[2];
            String sex = split[3];
            String clazz = split[4];

//            System.out.println(name);
//            System.out.println(age);
//            System.out.println(sex);
//            System.out.println(clazz);

            HashMap<String,String> map = new HashMap<String,String>();

            map.put("name",name);
            map.put("age",age);
            map.put("sex",sex);
            map.put("clazz",clazz);

//            System.out.println(map);

            jedis.hmset(id,map);
        }

After repeated inspection, there is no problem
with the code. The error is as follows:

The display array is out of bounds, followed by a 1. Baidu did not find similar problems and solutions, so I can only think about it slowly.
Then output the split characters and character length, found the problem, the length of the last line is always 1.

I think it should be a problem with the content of the txt file

and looked carefully and found the problem, the last two lines hit enter more, delete the problem That's it,
I hope that friends with similar problems can see it.

 

Guess you like

Origin blog.csdn.net/qq_61324603/article/details/130677520