java代码用eclipse编译异常!

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class NumricRange {
public static void main(String... args) throws Exception {
int[] numrics = new int[1000];
File file = new File("range-file.txt");
PrintStream out = new PrintStream(file);
out.println("562 ~602 928 ~968 671 ~711 819 ~859 302 ~342 532 ~572 958 ~998");
out.println("453 ~493 671 ~711 193 ~233 931 ~971 54 ~94 349 ~389 775 ~815");
out.close();
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
String[] groups = line.split("[\t ]+(?!~)");
if (groups != null)
for (String group : groups) {
String[] ranges = group.split("[\t ]*~");
int min = Integer.parseInt(ranges[0]);
int max = Integer.parseInt(ranges[1]);
for (int i = min; i <= max; i++)
numrics[i]++;
}
}
Map> result = new TreeMap>();
int lastTimes = -1;
int[] lastResult = null;
for (int i = 0; i < 1000; i++) {
int time = numrics[i];
if (time == lastTimes)
lastResult[1] = i;
else if (time > -1) {
List list = result.get(time);
if (list == null) {
list = new ArrayList();
result.put(time, list);
}
list.add(lastResult = new int[] { i, i });
lastTimes=time;
}
}
Iterator>> it = result.entrySet().iterator();
while (it.hasNext()) {
Entry> entry = it.next();
int time = entry.getKey();
List list = entry.getValue();
System.out.println("出现" + time + "次的范围:");
for (int[] range : list) {
System.out.print(range[0] + "~" + range[1] + " ");
}
System.out.println();
}
}
}

6个回答

 package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class NumricRange {

    public static void main(String[] args) throws Exception {
        int[] numrics = new int[1000];
        File file = new File("range-file.txt");
        PrintStream out = new PrintStream(file);
        out.println("562 ~602 928 ~968 671 ~711 819 ~859 302 ~342 532 ~572 958 ~998");
        out.println("453 ~493 671 ~711 193 ~233 931 ~971 54 ~94 349 ~389 775 ~815");
        out.close();
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            String[] groups = line.split("[\t ]+(?!~)");
            if (groups != null)
                for (String group : groups) {
                    String[] ranges = group.split("[\t ]*~");
                    int min = Integer.parseInt(ranges[0]);
                    int max = Integer.parseInt(ranges[1]);
                    for (int i = min; i <= max; i++)
                        numrics[i]++;
                }
        }
        TreeMap<Integer, List> result = new TreeMap<>();
        int lastTimes = -1;
        int[] lastResult = null;
        for (int i = 0; i < 1000; i++) {
            int time = numrics[i];
            if (time == lastTimes)
                lastResult[1] = i;
            else if (time > -1) {
                List list = result.get(time);
                if (list == null) {
                    list = new ArrayList();
                    result.put(time, list);
                }
                list.add(lastResult = new int[] { i, i });
                lastTimes = time;
            }
        }
        Iterator<Entry<Integer, List>> it = result.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Integer, List> entry = it.next();
            int time = entry.getKey();
            List<int[]> list = entry.getValue();
            System.out.println("出现" + time + "次的范围:");
            for (int[] range : list) {
                System.out.print(range[0] + "~" + range[1] + " ");
            }
            System.out.println();
        }
    }
}

qq_41811552
qq_41811552 上面代码运行了,有几个警告,没有错误,能运行出数据来!劳驾您看看能把警告去掉吗?
接近 2 年之前 回复

1、检查代码代码本身有没有错误
2、重启eclipse,看eclipse重新编译的代码是否有错误
3、如果编译不报错应该就是jdk版本的问题吧

1、检查代码是否有问题 2、关掉重新打开eclipse,eclipse会重新编译代码

编译不报错运行报错,使用的jdk版本过低

首先你说的是编译异常,eclipse在导入代码时会对代码进行预编译,会将一些非运行时异常直接报红色,我将你的代码重导一边,发现你的代码本身就有很多写错了,或者说是不完整,你的包也没导错,就是在处理集合上,集合的泛型没有写清楚,在这里我给你标出来了
public class NumricRange {
public static void main(String... args) throws Exception {
int[] numrics = new int[1000];
File file = new File("range-file.txt");
PrintStream out = new PrintStream(file);
out.println("562 ~602 928 ~968 671 ~711 819 ~859 302 ~342 532 ~572 958 ~998");
out.println("453 ~493 671 ~711 193 ~233 931 ~971 54 ~94 349 ~389 775 ~815");
out.close();
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
String[] groups = line.split("[\t ]+(?!~)");
if (groups != null)
for (String group : groups) {
String[] ranges = group.split("[\t ]*~");
int min = Integer.parseInt(ranges[0]);
int max = Integer.parseInt(ranges[1]);
for (int i = min; i <= max; i++)
numrics[i]++;
}
}
1. Map result = new TreeMap();
int lastTimes = -1;
int[] lastResult = null;
for (int i = 0; i < 1000; i++) {
int time = numrics[i];
if (time == lastTimes)
lastResult[1] = i;
else if (time > -1) {
List list = result.get(time);
if (list == null) {
list = new ArrayList();
result.put(time, list);
}
list.add(lastResult = new int[] { i, i });
lastTimes=time;
}
}
Set> entrySet = result.entrySet();
2. Iterator> it = entrySet.iterator();
while (it.hasNext()) {
Entry entry = it.next();
int time = (int) entry.getKey();
3. List list = (List) entry.getValue();
System.out.println("出现" + time + "次的范围:");
for (int[] range : list) {
System.out.print(range[0] + "~" + range[1] + " ");
}
System.out.println();
}
}
}
如果不是这些编译错误,可能是您的jdk没装好,或是jdk版本太低

qq_41811552
qq_41811552 上面代码运行了,出现好多错误,问题已经解决,谢谢您了!
接近 2 年之前 回复

先看有无语法错误,,没有的话看看环境有木有问题,最后看看逻辑错误

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问