简体   繁体   中英

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

Sorry this is my first post I need help on how to convert the month name "january" to an integer "1 " and how to make the dates equal, when I run it it returns that they are dont match.

To convert a month's name to it's integer value you can actually use Month#valueOf method:

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

Hint

They are not equals because you check with the same value m = 0 if you make this in your code :

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

You will get a result like this for the first dates:

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

Try to fix that before, and every thing will be good.

To convert the String of the month to an integer you could use a switch statement like this:

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

I would make an enum of Months. Then you can use the enum ordinal + 1 to get your integer.

public enum Months {
   JANURARY, FEBUARY ...
}

Then when you need to get the number that corresponds to the month

Months.JANUARY.ordinal() + 1;

Use the java.util.Calendar class. It has a get method that you can make use of. For ex: Calendar.get(Calendar.MONTH) which gives you with a value of the date that your calendar instance is representing. You can obtain a Calendar instance by Calendar.getInstance() . This will provide you with a Calendar instance that has a Locale and TimeZone that matches your system time. You also have options to get a customized Locale and TimeZone depending on your needs.

When you create an Date object using Date(String, int, int) constructor you leave its int month value ( m variable) default (0 in this case). That's why equal method doesn't give you the result you'd expect it to.

To resolve the problem you simply need to set m variable in the Date(String, int, int) based on given string like this:

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

you can write helper method to convert your particular string to particular 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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM