import java.io.*; import java.util.*; public class WordCount { public static void main(String[] args) throws IOException { // 读取文件内容 FileReader reader = new FileReader("四级常用单词.txt"); BufferedReader br = new BufferedReader(reader); String line; //接收一行 StringBuilder sb = null; while ((line = br.readLine()) != null) { line.trim(); String[] split = line.split("[^a-zA-Z]"); if (split[0] != null){ sb.append(split[0]); } } String str = sb.toString(); br.close(); reader.close(); int temp; int [] letters=new int[26] ; String words = str.toLowerCase(); for (int i = 0; i < words.length(); i++) { char c = words.charAt(i); letters[c - 'a'] ++; for (int i1 = 0; i1 < 26; i1++) { System.out.println((char) (i1 + 'a') + letters[i]); } } 更改代码中存在的错误
时间: 2024-01-23 07:01:52 浏览: 69
在代码中存在一个错误,即StringBuilder对象sb未初始化,需要进行初始化,可以将其赋值为一个新的StringBuilder对象,如下所示:
```
StringBuilder sb = new StringBuilder();
```
另外,还需要将输出语句中的letters[i]改为letters[i1],如下所示:
```
System.out.println((char) (i1 + 'a') + letters[i1]);
```
相关问题
package wc; import java.io.IOException; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class WordCountMain { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Configuration conf = new Configuration(); conf.set("fs.default.name","hdfs://localhost:9000"); String[] otherArgs = new String[]{"input","output"}; /* 直接设置输入参数 */ if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in><out>"); System.exit(2); } Job job = Job.getInstance(conf,"Merge and duplicate removal"); job.setJarByClass(WordCountMapper.class); job.setMapperClass(Map.class); job.setCombinerClass(WordCountReduce.class); job.setReducerClass(WordCountReduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } package wc; import java.io.IOException; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.io.Text; public class WordCountMapper{ public static class Map extends Mapper<Object, Text, Text, Text>{ private static Text text = new Text(); public void map(Object key, Text value, Context context) throws IOException,InterruptedException{ text = value; context.write(text, new Text("")); } } } job.setmapperclass出错
在您的代码中,您定义了一个名为 WordCountMapper 的类,该类实际上应该是一个 Mapper 类,并且应该继承 Mapper 接口。但是,在您的代码中,WordCountMapper 类没有继承 Mapper 接口,因此在设置 Mapper 类时出现了错误。
您需要将 WordCountMapper 类修改为一个 Mapper 类,并且让它继承 Mapper 接口,如下所示:
```
package wc;
import java.io.IOException;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.Text;
public class WordCountMapper extends Mapper<Object, Text, Text, Text> {
private static Text text = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
text = value;
context.write(text, new Text(""));
}
}
```
然后,您需要将 `job.setMapperClass(Map.class)` 修改为 `job.setMapperClass(WordCountMapper.class)`,以设置正确的 Mapper 类。
帮我解释下面的代码:import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); 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()); 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); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
这段代码是一个 Hadoop MapReduce 的 WordCount(词频统计)程序,主要包括以下几个部分:
1. 引入需要用到的类库:
```
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
```
2. 定义 Mapper 类:
```
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
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());
context.write(word, one);
}
}
}
```
Mapper 类主要是将输入的文本分割成单词,然后对每个单词输出一个键值对,其中键为单词,值为 1。
3. 定义 Reducer 类:
```
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);
}
}
```
Reducer 类主要是将 Mapper 输出的键值对按照键进行分组,然后对每组键值进行统计,输出每个单词出现的次数。
4. 定义主函数:
```
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
```
主函数主要是设置 MapReduce 作业的相关参数,包括输入路径、输出路径、Mapper 类、Reducer 类、输出键值对类型等。最后调用 `waitForCompletion` 函数提交作业并等待作业完成。
阅读全文