使用 split 时 Java 中的 ArrayIndexOutOfBoundsException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9859669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayIndexOutOfBoundsException in Java when using split
提问by user1291156
I am trying to read file and split its line to get some context(Computer Name and Date), the code gives few lines of outputs then gives the following exception:
我正在尝试读取文件并拆分其行以获取一些上下文(计算机名称和日期),代码给出了几行输出,然后给出了以下异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at FILE_MAIN.getComputerName(FILE_MAIN.java:34)
at FILE_MAIN.readFiles(FILE_MAIN.java:24)
at FILE_MAIN.main(FILE_MAIN.java:12)
Code:
代码:
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class FILE_MAIN
{
public static void main(String[] args) throws FileNotFoundException
{
File folder = new File("folderName/");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
readFiles(listOfFiles[i].getName());
}
}
public static void readFiles(String fileName) throws FileNotFoundException
{
FileReader dataFile = new FileReader("yukt/"+fileName);
try (Scanner FileRead = new Scanner(dataFile))
{
while (FileRead.hasNextLine() && FileRead.nextLine().isEmpty()==false)
{
String[] split;
String line = FileRead.nextLine();
split = line.split("\|",-1);
String computerName=getComputerName(split[0]);
System.out.println(computerName);
}
}
}
public static String getComputerName(String splited)
{
String[] split1;
String[] split2;
split1=splited.split("\:",-1);
split2=split1[1].split("\ ",-1);
return("a");
}
public static String getDate(String splited)
{
String[] split1=splited.split("\(",-1);
String[] split2=split1[1].split("\ ",-1);
return(split2[0]);
}
}
The main function gets names of the files in a folder, and passes each file to the readFiles function where each line is split into 3 parts by a delimeter(|) and parts are send to getComputerName and getDate function which returns its values after further splitting the strings.
main 函数获取文件夹中文件的名称,并将每个文件传递给 readFiles 函数,其中每行被分隔符 (|) 分成 3 部分,部分被发送到 getComputerName 和 getDate 函数,该函数在进一步拆分后返回其值字符串。
Here is an example of a line of the file, all the lines are similar to this:
这是文件中一行的示例,所有行都类似于:
[CD8C] ComputerName:NITIN UserID:GO ankurjain Station 9900 LanId: | (11/24 19:50:30) | Client is disconnected from agent.
回答by hmjd
There is no protection for split1
containing a single element:
split1
包含单个元素没有保护:
split1=splited.split("\:",-1);
split2=split1[1].split("\ ",-1); // Accesses second element of split1
Add protection and decide if it is an error for there to be no :
in the input string or just use whole string if no :
:
添加保护并确定:
输入字符串中没有是否是错误,或者如果没有则仅使用整个字符串:
:
split1=splited.split("\:",-1);
if (split1.length > 1)
{
split2=split1[1].split("\ ",-1);
}
回答by Benjamin Udink ten Cate
split2=split1[1]
gives you java.lang.
ArrayIndexOutOfBoundsException: 1
the error. The Array does not have 2 elements so index on 1 will throw an error.
split2=split1[1]
给你java.lang.
ArrayIndexOutOfBoundsException: 1
错误。Array 没有 2 个元素,因此索引 1 会引发错误。
You could add a check to make sure it has atleast 2 elements by putting the assignement in a if statement
您可以通过将赋值放在 if 语句中来添加检查以确保它至少有 2 个元素
if (split1.lenght > 1){
split2=split1[1].split("\ ",-1);
}
回答by John3136
split1=splited.split("\:",-1);
split2=split1[1].split("\ ",-1);
split1 must not be doing what you think. i.e. it is not splitting, cos split1[1] is not valid.
split1 不能按照您的想法行事。即它不是分裂,cos split1[1] 是无效的。
You should really check the result of the first split before trying to use it's results.
在尝试使用它的结果之前,您应该真正检查第一次拆分的结果。
回答by Tarun
I had similar issue where I had to check weather string sub part contains given string or not. But String in question had many variation. Instead of using if loop I used ternary operator -
我有类似的问题,我必须检查天气字符串子部分是否包含给定的字符串。但是有问题的 String 有很多变化。我没有使用 if 循环,而是使用了三元运算符 -
StringUtils.containsIgnoreCase("Test String",
("split me".split(":").length > 1)
? "split me".split(":")[1] : "Can't split")