title: MapReduce: String index out of Range
body: <p>I have a Problem with the MapReduce-Code. It is saying that my String-Index is out of Range, but the String is long enough for sure. 
  Does anyone have any suggestions? Thanks!</p>
  
  <p>This is my Code:</p>
  
  <p>`   </p>
  
  <pre><code>package Test;
  import java.io.IOException;
  import org.apache.hadoop.io.IntWritable;
  import org.apache.hadoop.io.Text;
  import org.apache.hadoop.mapreduce.Mapper;
  
  public class TestMapper extends     Mapper&lt;Text,Text,IntWritable,IntWritable&gt; {
  private IntWritable date_int = null;
  private IntWritable amount_int = null; 
  public void map(Text key, Text value, Context context)
          throws IOException, InterruptedException {
      String date_str = value.toString().substring(4,5);
      String amount_str = value.toString().substring(7,8);
      date_int = new IntWritable(Integer.parseInt(date_str));
      amount_int = new IntWritable(Integer.parseInt(amount_str));
      // Sammeln der Ergebnisse
      context.write(date_int, amount_int);
      }
  }
  
  package Test
  
  import java.io.IOException;
  
  import org.apache.hadoop.io.FloatWritable;
  import org.apache.hadoop.io.IntWritable; 
  //import org.apache.hadoop.io.Text;
  import org.apache.hadoop.mapreduce.Reducer;
  //import org.apache.hadoop.mapreduce.Reducer.Context;
  
  public class TestReducer extends Reducer&lt;IntWritable, IntWritable,
  IntWritable, FloatWritable&gt; {
      public void reduce(IntWritable key, Iterable&lt;IntWritable&gt; values, Context   context)
              throws IOException, InterruptedException {  
          float sum = 0;
          int count = 0;
          for (IntWritable val : values) { sum +=val.get();
          count +=1;
          }   
          float result = sum / count;
          context.write(key, new FloatWritable(result));
  
           }
       }
  
  
  
  package Test;
  
  import java.io.IOException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import org.apache.hadoop.conf.Configuration;
  import org.apache.hadoop.conf.Configured;
  import org.apache.hadoop.fs.Path;
  import org.apache.hadoop.io.FloatWritable;
  import org.apache.hadoop.io.IntWritable;
  import org.apache.hadoop.mapreduce.Job;
  import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
  import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  import org.apache.hadoop.util.Tool;
  import org.apache.hadoop.util.ToolRunner;
  public class TestDriver extends Configured implements Tool {
  private final static Logger log =       Logger.getLogger(TestDriver.class.getName());
  public static void main(String[] args) {
  int res = 1; // Wenn 1 nicht veraendert wird, endet der Job nicht korrekt
  try {
  res = ToolRunner.run(new Configuration(), new TestDriver(), args);
  } catch (Exception e) {
  log.log(Level.SEVERE, "Fehler beim Ausfuehren des Jobs!");
  e.printStackTrace();
      }
      System.exit(res);
      }
  @Override
  public int run(String[] args) {
  log.log(Level.INFO, "Start Map-Reduce-Job 'TestDriver'... ");
  Configuration conf = this.getConf();
  Job job = null;
  try {
  job = Job.getInstance(conf);
  } catch (IOException e1) {
  log.log(Level.SEVERE, "Fehler bei Instanziierung des Jobs!");
  e1.printStackTrace();
  }
  
  job.setJarByClass(TestDriver.class);
  job.setMapperClass(TestMapper.class);
  job.setReducerClass(TestReducer.class);
  job.setOutputKeyClass(IntWritable.class);
  job.setOutputValueClass(FloatWritable.class);
  job.setMapOutputKeyClass(IntWritable.class);
  job.setMapOutputValueClass(IntWritable.class);
  job.setInputFormatClass(KeyValueTextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);
  
  try {
  FileInputFormat.addInputPath(job, new Path(args[0]));
  } catch (IllegalArgumentException e) {
  log.log(Level.SEVERE, "Fehler (Argument) beim Setzen des     Eingabepfades!");
  e.printStackTrace();
  } catch (IOException e) {
  log.log(Level.SEVERE, "Fehler (IO) beim Setzen des Eingabepfades!");
  e.printStackTrace();
  }
  
  FileOutputFormat.setOutputPath(job, new Path(args[1]));
  boolean result = false;
  try {
  
  result = job.waitForCompletion(true);
  } catch (ClassNotFoundException e) {
  log.log(Level.SEVERE, "Fehler (ClassNotFound) beim Ausfuehren des     Jobs!");
  e.printStackTrace();
  } catch (IOException e) {
  log.log(Level.SEVERE, "Fehler (IOException) beim Ausfuehren des Jobs!");
  e.printStackTrace();
  } catch (InterruptedException e) {
  log.log(Level.SEVERE, "Fehler (Interrupted) beim Ausfuehren des Jobs!");
  e.printStackTrace();
  }
  log.log(Level.INFO, "Fertig!");
  return result ? 0 : 1;
  }
  
  }
  </code></pre>
  
  <p>`</p>
  
  <p>And this is the Error Message:</p>
  
  <p>java.lang.Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 5
      at org.apache.hadoop.mapred.LocalJobRunner$Job.runTasks(LocalJobRunner.java:462)
      at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:522)</p>
  
  <p>My Input File is a Text File and it is like that: </p>
  
  <pre><code>200912024
  2009120420
  2009120750
  200912083
  2009120912
  2009121066
  2009121170
  2009121225
  2009121430
  2009121560
  2009121621
  2009121722
  2009121818
  2009122112
  2009122213
  </code></pre>
  
  <p>Thanks!</p>