public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
int left = line.indexOf("{");
int right = line.indexOf("}");
String subMyString = line.substring(left+1, right);
for (String myWord : subMyString.split("\\W+")) {
if (myWord.length() > 0)
context.write(new Text(myWord), new IntWritable(1));
}
}在我的mapper.class里
我的输入文件:
...
...bla..bla..{asd assda sddsaasd asd}
...bla..bla..{asd assda sddsaasd asd}
...bla..bla..{asd assda sddsaasd asd}
...自然:所有行中的line= ...bla..bla..{asd assda sddsaasd asd}都包括"{“和"}”字符。我要在这些字符之间填入contex。但是我在编译过程中得到了java.lang.StringIndexOutOfBoundsException。
我该如何修改我的代码?为什么我会犯错?
谢谢。
发布于 2014-06-03 20:38:54
从您提供的代码中,有两种方法可以获得java.lang.StringIndexOutOfBoundsException
{和}是不平衡的。{和}的情况。因为,如果它们不在值中,则left和right变量将成为-1。因此你得到了一个StringIndexOutOfBoundsException因此,您应该更改的代码如下:
//......
int left = line.indexOf("{");
int right = line.indexOf("}");
if(left > 0 && right > 0){
String subMyString = line.substring(left+1, right);
for (String myWord : subMyString.split("\\W+")) {
if (myWord.length() > 0)
context.write(new Text(myWord), new IntWritable(1));
}
}
//..........https://stackoverflow.com/questions/23742707
复制相似问题