-1
下面的代碼給我"java.lang.StringIndexOutOfBoundsException: String index out of range: 14"
代碼提供「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);
}
}
}
字符串中的第15個字符不存在。你的字符串比這短。 – Bathsheba
每個單字都有21個或更多字符? – SMA
[Java子字符串:字符串索引超出範圍]可能的重複(http://stackoverflow.com/questions/953527/java-substring-string-index-out-of-range) – xenteros