ArrayIndexOutOfBoundsException 将 C# 转换为 Java
ArrayIndexOutOfBoundsException converting C# to Java
我正在开发一个程序,它将遍历记录列表(ID 和票证)并将它们分别解析为两个列表。它还将交叉搜索列表以查看哪些 ID 具有基于名称的相应工单。这是早期版本的 link:here
现在,我一直在同事的一些 C# 代码的帮助下重写,但我在解析方法方面遇到了麻烦。这是 C# 版本:
public void parseLine(string _line)
{
if(string.IsNullOrWhiteSpace(_line)){ return;}
code = _line.Substring(0, 3);
ticketID = _line.Substring(3, 10);
string tmp = _line.Substring(13).Trim();
//get the first and last name
string [] tmp1 = tmp.Split(",".ToCharArray());
if(!(tmp1.Length > 1))
{
throw new Exception("unable to get the first and last name");
}
lastname = tmp1[0];
firstname = tmp1[1].Trim();
}
这是我的 Java 版本:
public void parseLine(String line) throws Exception {
// code will store Ticket code *Alpha ticketId will store 10
// *digit code
code = line.substring(0, 3);
ticketId = line.substring(3, 10);
// tmp will store everything afterthe first 13 characters of
// line and trim the name(s)
String tmp = line.substring(13).trim();
// tmp1 array
String[] tmp1 = tmp.split(".*,.*");
if (tmp1.length > 1) {
throw new Exception("UNABLE TO GET NAME");
}
last = tmp1[0];
first = tmp1[1].trim();
}
这是在一个单独的 class 中,它将为有票的人建模。我调用实际 parseLine 方法的主要 class(到目前为止)如下:
public class ParkingTickets {
public static void main(String[] args) throws
FileNotFoundException, Exception {
ArrayList<TicketPeople> tickets = new ArrayList<>();
HashMap<String, List<SbPeople>> people = new HashMap<>();
File srcFile = new File("source.txt");
Scanner myScanner = new Scanner(srcFile);
while (myScanner.hasNextLine()) {
String line = myScanner.nextLine();
//System.out.println(line);
if (line.matches("^\p{Alpha}.*$")) {
//System.out.printf("Ticket: %s%n", line);
TicketPeople t = new TicketPeople();
t.parseLine(line);
tickets.add(t);
}
myScanner.close();
}
}
}
编译器指向 parseLine 方法中的 if 语句,显然是 main class 中的 parseLine 方法,当我尝试单步执行该 sepcifiv 行时,我看到它开始解析来自源文件,但有问题。从文档中,错误意味着:抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。
我将 ArrayList 用于票证列表,据我所知,它是一个动态列表,不需要设置特定的索引大小。我仍在学习并且无法理解此异常。如果有任何帮助,我将不胜感激。
您在 Java 中对 split()
的调用与 C# 中的 split()
不匹配。
// String[] tmp1 = tmp.split(".*,.*");
String[] tmp1 = tmp.split(","); // <-- ",".
还有,你的校验逻辑好像反了。但是,我建议
if (tmp1.length != 2) {
throw new Exception("UNABLE TO GET NAME");
}
1) 在 C# 中是 Substring(int startIndex, int length)。 In java String substring(int startindex, int endindex)。
2)java代码也改变了异常逻辑。在 C# 代码中没有 if(!(tmp1.Length > 1)),而在 java 代码中,if (tmp1.length > 1)
我正在开发一个程序,它将遍历记录列表(ID 和票证)并将它们分别解析为两个列表。它还将交叉搜索列表以查看哪些 ID 具有基于名称的相应工单。这是早期版本的 link:here 现在,我一直在同事的一些 C# 代码的帮助下重写,但我在解析方法方面遇到了麻烦。这是 C# 版本:
public void parseLine(string _line)
{
if(string.IsNullOrWhiteSpace(_line)){ return;}
code = _line.Substring(0, 3);
ticketID = _line.Substring(3, 10);
string tmp = _line.Substring(13).Trim();
//get the first and last name
string [] tmp1 = tmp.Split(",".ToCharArray());
if(!(tmp1.Length > 1))
{
throw new Exception("unable to get the first and last name");
}
lastname = tmp1[0];
firstname = tmp1[1].Trim();
}
这是我的 Java 版本:
public void parseLine(String line) throws Exception {
// code will store Ticket code *Alpha ticketId will store 10
// *digit code
code = line.substring(0, 3);
ticketId = line.substring(3, 10);
// tmp will store everything afterthe first 13 characters of
// line and trim the name(s)
String tmp = line.substring(13).trim();
// tmp1 array
String[] tmp1 = tmp.split(".*,.*");
if (tmp1.length > 1) {
throw new Exception("UNABLE TO GET NAME");
}
last = tmp1[0];
first = tmp1[1].trim();
}
这是在一个单独的 class 中,它将为有票的人建模。我调用实际 parseLine 方法的主要 class(到目前为止)如下:
public class ParkingTickets {
public static void main(String[] args) throws
FileNotFoundException, Exception {
ArrayList<TicketPeople> tickets = new ArrayList<>();
HashMap<String, List<SbPeople>> people = new HashMap<>();
File srcFile = new File("source.txt");
Scanner myScanner = new Scanner(srcFile);
while (myScanner.hasNextLine()) {
String line = myScanner.nextLine();
//System.out.println(line);
if (line.matches("^\p{Alpha}.*$")) {
//System.out.printf("Ticket: %s%n", line);
TicketPeople t = new TicketPeople();
t.parseLine(line);
tickets.add(t);
}
myScanner.close();
}
}
}
编译器指向 parseLine 方法中的 if 语句,显然是 main class 中的 parseLine 方法,当我尝试单步执行该 sepcifiv 行时,我看到它开始解析来自源文件,但有问题。从文档中,错误意味着:抛出以指示已使用非法索引访问数组。索引为负数或大于或等于数组的大小。 我将 ArrayList 用于票证列表,据我所知,它是一个动态列表,不需要设置特定的索引大小。我仍在学习并且无法理解此异常。如果有任何帮助,我将不胜感激。
您在 Java 中对 split()
的调用与 C# 中的 split()
不匹配。
// String[] tmp1 = tmp.split(".*,.*");
String[] tmp1 = tmp.split(","); // <-- ",".
还有,你的校验逻辑好像反了。但是,我建议
if (tmp1.length != 2) {
throw new Exception("UNABLE TO GET NAME");
}
1) 在 C# 中是 Substring(int startIndex, int length)。 In java String substring(int startindex, int endindex)。
2)java代码也改变了异常逻辑。在 C# 代码中没有 if(!(tmp1.Length > 1)),而在 java 代码中,if (tmp1.length > 1)