java扫描仪异常可能是数据太大了?

问题描述 投票:0回答:0

我在输入几千条数据时没有报错,但是当数据条数变为100000条时,我报错'Exception in thread "main" java.util.InputMismatchException'。我不确定这是否是 Scanner 的问题。谁能帮我解决这个问题?”

import java.math.BigInteger;
import java.util.*;

class Work {
    public long end;
    public long money;
    Work(long end, long money){
        this.end = end;
        this.money = money;
    }
}
public class TanXin {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        List<Work> works = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            long end = scanner.nextLong();
            long money = scanner.nextLong();
            works.add(new Work(end ,money));
        }
        Collections.sort(works, new Comparator<Work>() {
            @Override
            public int compare(Work o1, Work o2) {
                return Long.compare(o1.end, o2.end);
            }
        });
        scanner.nextLine();
        long curEnd =  works.get(0).end;
        long money = works.get(0).money;
        BigInteger result = BigInteger.valueOf(0);
        for (int i = 1; i < works.size(); i++) {
            Work work = works.get(i);
            if(work.end == curEnd){
                money = Math.max(money, work.money);
            }
            else {
                result=result.add(BigInteger.valueOf(money));
                money = work.money;
                curEnd = work.end;
            }
        }
        result=result.add(BigInteger.valueOf(money));
        System.out.println(result);
    }
}

我的输入是这样的:

100000
801615855 349230664 887145852 256709348 543490607 406712523 290306792 866994656 761434274 614622174 523543132 477488683 416651127 515580101 610336950 152185241 337713494 547698342 6453239 251894056 17161871 278487265 309123183 826630026 22171935 97670396 937046566 551871611 338360787 511206428 234499030 12274630 999066863 377262036 498450141 907352279 699130053 40056368 341503872 598356413 92329093 58969313 839293680 912223354 435852220 370051435 328226392 703616380 84394712 320189993 89139302 692379179 668921100 37915667 750500527 980565603 951214595 443240740 342283327 733767608 674959282 407118651 114057416 595985258 908001458 748864346 406064066 170499053

java java.util.scanner
© www.soinside.com 2019 - 2024. All rights reserved.