簡體   English   中英

如何將String轉換為int?

[英]How can I make convert a String to int?

public class Date {

    private int m; // month
    private int d; // day
    private int y; // year

    private String month; // string month name ex. "January"
    private int day; // int day
    private int year;// year

    private boolean equals; // make the dates equal 

    public Date(int m, int d, int y) {
        this.m = m;
        this.d = d;
        this.y = y;
    }

    public Date(String month, int d, int y) {
        this.month = month;
        this.d = d;
        this.y = y;
    }

    public boolean equals(Object other) {
        if (!(other instanceof Date)) {
            return false;
        }

        Date d2 = (Date) other;

        if (y == d2.y) {

            if (m == d2.m) {

                if (d == d2.d) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public String toString() {

        if (m == 1) {
            return "January " + d + ", " + y;
        } else if (m == 2) {
            return "February " + d + ", " + y;
        } else if (m == 3) {
            return "March " + d + ", " + y;
        } else if (m == 4) {
            return "April " + d + ", " + y;
        } else if (m == 5) {
            return "May " + d + ", " + y;
        } else if (m == 6) {
            return "June " + d + ", " + y;
        } else if (m == 7) {
            return "July " + d + ", " + y;
        } else if (m == 8) {
            return "August " + d + ", " + y;
        } else if (m == 9) {
            return "Septmember " + d + ", " + y;
        } else if (m == 10) {
            return "October " + d + ", " + y;
        } else if (m == 11) {
            return "November " + d + ", " + y;
        } else if (m == 12) {
            return "December " + d + ", " + y;
        } else {
            return month + " " + d + ", " + y;
        }

    }

}



import java.util.Random;


public class DateDemo {

    public static void test(Date d1, Date d2) {
        if (d1.equals(d2)) {
            System.out.println("\"" + d1 + "\" matches \"" + d2 + "\"");
        } else {
            System.out.println("\"" + d1 + "\" doesn't matche \"" + d2 + "\"");
        }
    }

    public static void main(String[] args) {
        test(new Date("January", 1, 1970), new Date(1, 1, 1970));
        test(new Date("December", 20, 1970), new Date(12, 20, 1971));
        test(new Date("July", 15, 1970), new Date(7, 14, 1970));
        test(new Date("July", 15, 1970), new Date(7, 15, 1970));
        test(new Date("November", 1, 1970), new Date(11, 1, 1970));
        test(new Date("April", 1, 1492), new Date(4, 1, 1492));
        test(new Date("March", 1, 1970), new Date(1, 1, 1970));

        Date d = new Date("January", 1, 1970);
        if (d.equals(new String("Blah"))) {
            System.out.println("Should not see me");
        } else {
            System.out.println("Date can only possibly match another Date");
        }
    }
}

抱歉,這是我的第一篇文章,我需要有關如何將月份名稱“ january”轉換為整數“ 1”以及如何使日期相等的幫助,當我運行它時,它將返回它們不匹配的信息。

要將月份的名稱轉換為整數值,您實際上可以使用Month#valueOf方法:

Month month = Month.valueOf("January".toUpperCase());
System.out.println(month.getValue() + " : " + month);
// 1 : JANUARY

暗示

它們不相等,因為如果在代碼中進行檢查,則您將使用相同的值m = 0進行檢查:

System.out.println(y + "<--------->" + d2.y);
if (y == d2.y) {
    System.out.println(m + "<--------->" + d2.m);
    if (m == d2.m) {

您將在第一個日期得到如下結果:

1970<--------->1970
0<--------->1

嘗試先解決此問題,一切都會好起來。

要將月份的字符串轉換為整數,可以使用如下的switch語句:

public int convert (String month) {
    int number = 0;  //just for the compiler
    switch(month) {
        case "January": int = 1
                        break;
        case "February": int = 2
                        break;
        ...
        case "December": int = 12
                        break;
    }
    return number;
}

我會列舉一個月。 然后,您可以使用枚舉序數+ 1來獲取整數。

public enum Months {
   JANURARY, FEBUARY ...
}

然后,當您需要獲取與月份相對應的數字時

Months.JANUARY.ordinal() + 1;

使用java.util.Calendar類。 它具有可以使用的get方法。 例如: Calendar.get(Calendar.MONTH) ,它為您提供日歷實例表示的日期的值。 您可以通過Calendar.getInstance()獲得Calendar實例。 這將為您提供一個Calendar實例,該實例的Locale和TimeZone與您的系統時間相匹配。 您還可以根據需要選擇獲取自定義的區域設置和TimeZone。

使用Date(String, int, int)構造函數創建Date對象時Date(String, int, int)將其int month值( m變量)保留為默認值(在這種情況下為0)。 這就是為什么equal方法不能給您期望的結果的原因。

要解決該問題,您只需要根據給定的字符串在Date(String, int, int)設置m變量,如下所示:

public Date(String month, int d, int y) {
    this.month = month;
    this.m = monthFromStr(month);
    this.d = d;
    this.y = y;
}

private int monthFromStr(String str) {
    if(str.equalsIgnoreCase("january"))
       return 1;
    // ...
    throw new IllegalArgumentException("Unknown month!");
}

您可以編寫幫助程序方法以將您的特定字符串轉換為特定的int。

 import java.util.Random;
 class Date {

    private int m; // month
    private int d; // day
    private int y; // year

    private String month; // string month name ex. "January"
    private int day; // int day
    private int year;// year

    private boolean equals; // make the dates equal 

    public Date(int m, int d, int y) {
        this.m = m;
        this.d = d;
        this.y = y;
    }

    public Date(String month, int d, int y) {
        this.month = month;
        this.d = d;
        this.y = y;
    }

    public int converter(String s){
    int i=0;
    if (s.equals("January")){
        return 1;
    }
    if (s.equals("February")){
        return 2;
    }
    if (s.equals("March")){
        return 3;
    }
    if (s.equals("April")){
        return 4;
    }
    if (s.equals("May")){
        return 5;
    }
    if (s.equals("June")){
        return 6;
    }
    if (s.equals("July")){
        return 7;
    }
    if (s.equals("August")){
        return 8;
    }
    if (s.equals("Septmember")){
        return 9;
    }
    if (s.equals("October")){
        return 10;
    }
    if (s.equals("November")){
        return 11;
    }
    if (s.equals("December")){
        return 12;
    }
    return 0;
    }

    public boolean equals(Object other) {
        if (!(other instanceof Date)) {
            return false;
        }

        Date d2 = (Date) other;
        if (month!=null){
            if (y == d2.y) {
                 if (this.converter(month) == d2.m) {
                    if (d == d2.d) {
                        return true;
                } 
                else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
        }
        else
        {
            return false;
        }
    }

    public String toString() {

        if (m == 1) {
            return "January " + d + ", " + y;
        } else if (m == 2) {
            return "February " + d + ", " + y;
        } else if (m == 3) {
            return "March " + d + ", " + y;
        } else if (m == 4) {
            return "April " + d + ", " + y;
        } else if (m == 5) {
            return "May " + d + ", " + y;
        } else if (m == 6) {
            return "June " + d + ", " + y;
        } else if (m == 7) {
            return "July " + d + ", " + y;
        } else if (m == 8) {
            return "August " + d + ", " + y;
        } else if (m == 9) {
            return "Septmember " + d + ", " + y;
        } else if (m == 10) {
            return "October " + d + ", " + y;
        } else if (m == 11) {
            return "November " + d + ", " + y;
        } else if (m == 12) {
            return "December " + d + ", " + y;
        } else {
            return month + " " + d + ", " + y;
        }

    }

}
public class DateDemo {

    public static void test(Date d1, Date d2) {
        if (d1.equals(d2)) {
            System.out.println("\"" + d1 + "\" matches \"" + d2 + "\"");
        } else {
            System.out.println("\"" + d1 + "\" doesn't matche \"" + d2 + "\"");
        }
    }

    public static void main(String[] args) {
        test(new Date("January", 1, 1970), new Date(1, 1, 1970));
        test(new Date("December", 20, 1970), new Date(12, 20, 1971));
        test(new Date("July", 15, 1970), new Date(7, 14, 1970));
        test(new Date("July", 15, 1970), new Date(7, 15, 1970));
        test(new Date("November", 1, 1970), new Date(11, 1, 1970));
        test(new Date("April", 1, 1492), new Date(4, 1, 1492));
        test(new Date("March", 1, 1970), new Date(1, 1, 1970));

        Date d = new Date("January", 1, 1970);
        if (d.equals(new String("Blah"))) {
            System.out.println("Should not see me");
        } else {
            System.out.println("Date can only possibly match another Date");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM