Java - 比较作为数字的字符串

时间:2014-03-19 03:17:52

标签: java file

如何比较哪些是数字?

我将字符串保存在文件中:

"2000 12 7 0 2 -3.0"
"2000 7 7 0 2 -4.0"
"2013 7 23 20 59 25.5"

前五个值是日期(年,月,日,小时,分钟)。我需要按时间顺序对它们进行排序。

这是我的代码:

 class Task3 {

    BufferedWriter writer;
    BufferedReader reader;
    int licznik = 0;



    Task3() throws IOException {
        this.writer = new BufferedWriter(new FileWriter("c:\\zadanie.txt"));
        this.reader = new BufferedReader(new FileReader("c:\\zadanie.txt"));

    }

    public void WriteMeasur(Measur measur) throws IOException{

       //licznik++;
        writer.write(measur.toString());
        writer.newLine();
    }

    public void WriteChrono(Measur measur) throws IOException{
        List<String> lista = new ArrayList<>();

        String line;        
        while((line = reader.readLine()) != null){
            lista.add(line);            
        }
    }
}

class Time implements Serializable{
    int year, month, day, hour, minute;

    Time(int r, int m, int d, int h, int min){
        if(r <= 2014 && r > 1990)
            this.year = r;
        if(m <= 12)
            this.month = m;
        if(d <= 30 || (m == 2 && d < 29))
            this.day = d;
        if(h <= 24)
            this.hour = h;
        if(min <= 60)
            this.minute = min;
    }

    @Override
    public String toString(){
        return String.valueOf(year) + " " + String.valueOf(month) +  " " +String.valueOf(day) + " " +String.valueOf(hour) +  " " +String.valueOf(minute);

    }
}
class Measur implements Serializable{
    Time time;
    double temp;

    Measur(Time czas, double temp){
        this.time = czas;
        this.temp = temp;

    }

    @Override
    public String toString(){
        return time.toString() + " " +String.valueOf(temp);

    }

}

public class Main{
    public static void main(String args[]) throws IOException{

        //Argumenty klasy czas: rok, miesiac, dzien, godzina, minuty
        // Metody inicjujace klase czas

        Time czas_1 = new Time(2013,7,23,20,59);
        Time czas_3 = new Time(2000,07,7,25,2);
        Time czas_2 = new Time(2000,12,7,25,2);

        // Metody inicjujace klase pomiar

        Measur pomiar_1 = new Measur(czas_1, 25.5);
        Measur pomiar_2 = new Measur(czas_2, -3);
        Measur pomiar_3 = new Measur(czas_3, -4);


        Task3 zad3 = new Task3();

        zad3.WriteMeasur(pomiar_1);
        zad3.WriteMeasur(pomiar_2);
        zad3.WriteMeasur(pomiar_3);
        zad3.writer.close();

    }
}

我不知道如何排序。我想使用Collections.sort(),但在Strings 2000 12中小于2000 7。

有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:3)

我不确定这是不是一个好主意。但您可以使用Collections.sortComparator的支持来自定义在排序值时比较值的方式,并SimpleDateFormat将值转换为Date值,该值可以然后直接比较......例如......

List<String> values = new ArrayList<String>(25);
values.add("2013 7 23 20 59 25.5");
values.add("2000 12 7 0 2 -3.0");
values.add("2000 7 7 0 2 -4.0");

Collections.sort(values, new Comparator<String>() {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd");
    @Override
    public int compare(String o1, String o2) {
        int result = -1;
        try {
            Date d1 = sdf.parse(o1);
            Date d2 = sdf.parse(o2);
            result = d1.compareTo(d2);
        } catch (ParseException exp) {
            exp.printStackTrace();
        }
        return result;
    }
});

for (String value : values) {
    System.out.println(value);
}

答案 1 :(得分:1)

创建一个个人MyDate对象,在其构造函数中传递String并解析该字符串以初始化year, month, day, hour, min, sec字段。

然后为您的班级Comparator编写自定义MyDateComparator并编写逻辑以进行排序。

MyDateString对象存储在List中并使用`Collections.sort(myDateList,new MyDateComparator())

有关如何使用Comparators

的提示,请参阅The Ordering Collections Java Tutorials

答案 2 :(得分:1)

用空格分隔字符串作为分隔符,转换为整数数组然后排序?