2017-02-12 7 views
1

英数字以外の文字に基づいてレコードを分割し、各単語の最初の文字を数え、各単語の最初のアルファベットの合計を取得しようとしています。以下は実行しようとしたMapperクラスのロジックです。MapReduceコードのStringIndexOutOfBoundsException

public void map(LongWritable key, Text value, Context ctx) { 
    String line = value.toString(); 
    String[] split = line.split("\\W+"); 
    String firstChar; 
    for(String words: split) { 
     firstChar = String.valueOf(words.charAt(0)); 
     try { 
      ctx.write(new Text(firstChar), new IntWritable(1)); 
     } catch (IOException | InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

例外:

Error: java.lang.StringIndexOutOfBoundsException: String index out of range: 0 
    at java.lang.String.charAt(String.java:658) 
    at com.hadoopexp.mapper.MapperClass.map(MapperClass.java:17) 
    at com.hadoopexp.mapper.MapperClass.map(MapperClass.java:1) 

しかし、イムラインで、このロジックにStringIndexOutOfBounds例外を取得:

firstChar = String.valueOf(words.charAt(0)); 

私はそれならば、単に確認するために、入力ファイルにいくつかの空白行を入れています働く(以下のように)

Liverpool 
Manchester 


London 

Toronto ? ?? !!12 32 

誰でもロジックを修正する方法を教えてください。どんな助けでも本当に感謝しています。

答えて

0

空の文字列を分割すると、空の文字列の1つの要素を含む配列が返されます。私はそれを明示的にチェックするだけです:

for(String words: split) { 
    if (!words.isEmpty()) { // Here! 
     firstChar = String.valueOf(words.charAt(0)); 
     try { 
      ctx.write(new Text(firstChar), new IntWritable(1)); 
     } catch (IOException | InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

ありがとうMureinik。それは働いている。 – Sidhartha

関連する問題