Java-比较数字字符串 [英] Java - Comparing Strings which are numbers

查看:362
本文介绍了Java-比较数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何比较String是数字?

How to compare String which are numbers?

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

I have Strings saved in file:

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

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

First five values are date (year, month, day, hour, minute). I need to sort them chronologically.

这是我的代码:

 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.

I have no idea how to sort it. I wanted use Collections.sort(), but in Strings 2000 12 is smaller then 2000 7.

有什么建议吗?

谢谢!

推荐答案

我不确定这是否是个好主意.但是您可以使用 Comparator 来定制对值进行排序和

I'm not sure this is a good idea or not. But you could use Collections.sort support of Comparator to customise the way in which values are compared while they are been sorted and SimpleDateFormat to convert the value to a Date value which can then be compared directly...for example...

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);
}

这篇关于Java-比较数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆