守则给予「;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:14“;

守则给予「;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:14“;,java,string,hadoop,mapreduce,Java,String,Hadoop,Mapreduce,以下代码为我提供了“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:14” 请指导我的代码有什么问题 public class max_temp { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = ne

以下代码为我提供了“java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:14”

请指导我的代码有什么问题

public class max_temp {

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public String y;
        public String a;
        public Double t;

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                y = word.toString();
                a = y.substring(7,14);
                t = Double.parseDouble((y.substring(35,41).trim()));

                word.set(a);              

               // 27516201501012.424-156.6171.32-18.3-21.8-20.0-1   9.90.00.00C-19.2-24.5-21.983.973.777.                                
               context.write(word, one);
           }
       }
   }

   public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
       private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }
}
公共类最高温度{
公共静态类TokenizerMapper扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共字符串y;
公共字符串a;
公共双t;
公共void映射(对象键、文本值、上下文上下文)引发IOException、InterruptedException{
StringTokenizer itr=新的StringTokenizer(value.toString());
而(itr.hasMoreTokens()){
set(itr.nextToken());
y=单词.toString();
a=y.子串(7,14);
t=Double.parseDouble((y.substring(35,41).trim());
单词集(a);
//27516201501012.424-156.6171.32-18.3-21.8-20.0-19.90.00.00C-19.2-24.5-21.983.973.777。
上下文。写(单词,一);
}
}
}
公共静态类IntSumReducer扩展了Reducer{
私有IntWritable结果=新的IntWritable();
公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=val.get();
}
结果集(总和);
编写(键、结果);
}
}
}

如果附加stacktrace,会更容易。无论如何,问题是,您正在调用长度小于15的
字符串的
子字符串(7,14)
。Java不知道该做什么,因此抛出异常

问题在于你的应用程序逻辑。如果使用
子字符串(7,14)
必须确保
字符串足够长,或者使用
try catch

try {
    String s = s.substring(7,14);
} catch (StringIndexOutOfBoundsException e) {
    //somehow process the situation in which the string is too short.
}

字符串中的第15个字符不存在。您的字符串比那个短。每个单词是否有21个或更多字符?我的答案可能重复吗?如果是,请单击答案分数下方的灰色勾号,将其标记为解决方案。