虽然我明白,错误本身确实告诉我问题出在哪里,它在D2.main(D2.java:19)或第19行即时通讯无法弄清楚究竟是什么问题。我试图通过使用split(“:”)来遍历文件,以便以良好整洁的格式打印文件中的所有信息。虽然获得其他信息不是一个问题,但从中获得社会安全号码似乎产生了相当头痛的问题。下面是我的代码和任何指针或帮助将非常感激!为此工作了更多的小时,然后id喜欢承认,并希望它的第二双眼睛可能指出一些愚蠢的东西,我可以感觉后来没有意识到愚蠢。谢谢!Java.Lang.StringIndexOutOfBoundsException:字符串索引超出范围:5
import java.util.Scanner;
import java.io.*;
public class D2
{
public static void main(String[] args)throws FileNotFoundException
{
String categories, sections[];
int x;
Scanner reader = new Scanner(new FileReader(args[0]));
PrintWriter outputfile = new PrintWriter(args[1]);
while(reader.hasNext())
{
categories = reader.nextLine();
sections = categories.split(":");
String social = sections[3];
String k, l, p;
k = social.substring(0,3);
l = social.substring(3,5);
p = social.substring(5);
String ssn = k + "-" + l + "-" + p;
String top = ("Student Name SSN DOB CURR GRAD YR");
String middle = ("------------ --- --- ---- -------");
String bottom = (sections[0] + " " + ssn + " " + sections[4] + " " + sections[1] + " " + sections[2]);
outputfile.println(top + "\n" + middle + "\n" + bottom);
outputfile.close();
reader.close();
}
}
}
约翰·琼斯:987-65-4321:1956年3月12日:ELT:2005 费城约翰:876-54-3219:1989年12月15日:CIS:2003 威廉·斯威尼:768- 45-2768:01-03-1990:MAS:2006 Terry Hundred:734-85-9437:04-15-1965:BAD:2001 Susan Pine:489-37-2637:08-08-1988:INS: 2007
上面是我尝试读取的文本文件,输出文件的名称并不重要。
String social = sections[3]; //equals ELT
java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:5 \t在java.lang.String.substring(未知来源) \t在D2.main(D2.java:19) –
以上是错误 –