String Index Out Of Range: -1 是我在 hadoop MR 的映射器中执行此代码时遇到的错误。
String line = value.toString();
String[] longLatArray = line.split(",");
double lat = Double.parseDouble((longLatArray[0]).substring(1));
行看起来像这样:“(-21.24132363125682,-175.14134768396616,1)”
我的目标是删除第一个“(”并将 -21.2873623 放入我的 lat 变量中。
编辑:
当我尝试在 hadoop 界面中调试时,这是我得到的:
attempt_201406251613_0002_m_000000_0: line: (-21.24132363125682,-175.14134768396616,1)
attempt_201406251613_0002_m_000000_0: index 0
attempt_201406251613_0002_m_000000_0: (-21.24132363125682
attempt_201406251613_0002_m_000000_0: index 1
attempt_201406251613_0002_m_000000_0: -175.14134768396616
attempt_201406251613_0002_m_000000_0: index 2
attempt_201406251613_0002_m_000000_0: 1)
当我在本地执行非常相似的代码时,我没有得到任何错误
还有,这里的-1有什么意义?
如有任何帮助,我们将不胜感激!
最佳答案
public static void main(String[] args) throws Exception {
String value="(-21.2873623,160.2323,1)";
String line = value.toString();
String[] longLatArray = line.split(",");
double lat = Double.parseDouble((longLatArray[0]).substring(1));
System.out.println(lat);
}
这对我来说是按预期工作的,从 value
中的坐标中为我提供纬度。这使我相信 value
中的任何内容都存在问题。您确定该值包含您期望的内容吗?您可以通过执行以下操作进行调试:
System.out.println(value); // or use a logging library
并验证这一点。 -1 让我相信您已经过了字符串的末尾,但我对此不是 100% 肯定。
关于java - 字符串索引超出范围 : -1 error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414469/