Error: java.lang.NumberFormatException

Error: java.lang.NumberFormatException,第1张

Error: java.lang.NumberFormatException

今天在写代码的时候又出现了问题,完整报错如下:

Error: java.lang.NumberFormatException: For input string: "computer"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at S2.map2.map(map2.java:21)
        at S2.map2.map(map2.java:13)
        at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
        at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
        at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
        at org.apache.hadoop.mapred.YarnChild.run(YarnChild.java:164)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:422)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1758)
        at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)

首先,由上述报错的第一行可知是数据类型出现问题,然后再根据报错中提供的报错位置

"at S2.map2.map(map2.java:21)
 at S2.map2.map(map2.java:13)"去检查代码

package S2;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Locale;

//2、统计每门课程参考学生的平均分,并且按课程存入不同的结果文件,要求一门课程一个结果文件,并且按平均分从高到低排序,分数保留一位小数
public class map2 extends Mapper {
    private bean outk=new bean();
    @Override
    protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
        String[] str=value.toString().split(",");
        double sum=0;
        int num=0;
        for(int i=0;i< str.length;i++){
            sum+=Integer.parseInt(str[i]);
            num++;
        }
        double avg=Math.round(sum*1D/num*10)/10D;
        outk.setCourse(str[0]);
        outk.setName(str[1]);
        outk.setGrade(avg);
        context.write(outk,NullWritable.get());
    }
}

检查第21行发现,i的起始值应该是2,把它改过来再跑一次就过了

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3981617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存