Replacing String month with type Int - What to do!?!

import java.util.Scanner;
public class Date
    private String month;
    private int day;
    private int year; //a four digit number.
    public Date( )
        month = "January";
        day = 1;
        year = 1000;
    public Date(int monthInt, int day, int year)
        setDate(monthInt, day, year);
    public Date(String monthString, int day, int year)
        setDate(monthString, day, year);
    public Date(int year)
        setDate(1, 1, year);
    public Date(Date aDate)
        if (aDate == null)//Not a real date.
             System.out.println("Fatal Error.");
             System.exit(0);
        month = aDate.month;
        day = aDate.day;
        year = aDate.year;
    public void setDate(int monthInt, int day, int year)
        if (dateOK(monthInt, day, year))
            this.month = monthString(monthInt);
            this.day = day;
            this.year = year;
        else
            System.out.println("Fatal Error");
            System.exit(0);
    public void setDate(String monthString, int day, int year)
        if (dateOK(monthString, day, year))
            this.month = monthString;
            this.day = day;
            this.year = year;
        else
            System.out.println("Fatal Error");
            System.exit(0);
    public void setDate(int year)
        setDate(1, 1, year);
    public void setYear(int year)
        if ( (year < 1000) || (year > 9999) )
            System.out.println("Fatal Error");
            System.exit(0);
        else
            this.year = year;
    public void setMonth(int monthNumber)
        if ((monthNumber <= 0) || (monthNumber > 12))
            System.out.println("Fatal Error");
            System.exit(0);
        else
            month = monthString(monthNumber);
    public void setDay(int day)
        if ((day <= 0) || (day > 31))
            System.out.println("Fatal Error");
            System.exit(0);
        else
            this.day = day;
    public int getMonth( )
        if (month.equals("January"))
            return 1;
        else if (month.equals("February"))
            return 2;
        else if (month.equalsIgnoreCase("March"))
            return 3;
        else if (month.equalsIgnoreCase("April"))
            return 4;
        else if (month.equalsIgnoreCase("May"))
            return 5;
        else if (month.equals("June"))
            return 6;
        else if (month.equalsIgnoreCase("July"))
            return 7;
        else if (month.equalsIgnoreCase("August"))
            return 8;
        else if (month.equalsIgnoreCase("September"))
            return 9;
        else if (month.equalsIgnoreCase("October"))
            return 10;
        else if (month.equals("November"))
            return 11;
        else if (month.equals("December"))
            return 12;
        else
            System.out.println("Fatal Error");
            System.exit(0);
            return 0; //Needed to keep the compiler happy
    public int getDay( )
        return day;
    public int getYear( )
        return year;
    public String toString( )
        return (month + " " + day + ", " + year);
    public boolean equals(Date otherDate)
        return ( (month.equals(otherDate.month))
                  && (day == otherDate.day) && (year == otherDate.year) );
    public boolean precedes(Date otherDate)
        return ( (year < otherDate.year) ||
           (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
           (year == otherDate.year && month.equals(otherDate.month)
                                         && day < otherDate.day) );
    public void readInput( )
        boolean tryAgain = true;
        Scanner keyboard = new Scanner(System.in);
        while (tryAgain)
            System.out.println("Enter month, day, and year.");
              System.out.println("Do not use a comma.");
            String monthInput = keyboard.next( );
            int dayInput = keyboard.nextInt( );
            int yearInput = keyboard.nextInt( );
            if (dateOK(monthInput, dayInput, yearInput) )
                setDate(monthInput, dayInput, yearInput);
                tryAgain = false;
            else
                System.out.println("Illegal date. Reenter input.");
    private boolean dateOK(int monthInt, int dayInt, int yearInt)
        return ( (monthInt >= 1) && (monthInt <= 12) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    private boolean dateOK(String monthString, int dayInt, int yearInt)
        return ( monthOK(monthString) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    private boolean monthOK(String month)
        return (month.equals("January") || month.equals("February") ||
                month.equals("March") || month.equals("April") ||
                month.equals("May") || month.equals("June") ||
                month.equals("July") || month.equals("August") ||
                month.equals("September") || month.equals("October") ||
                month.equals("November") || month.equals("December") );
    private String monthString(int monthNumber)
        switch (monthNumber)
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            System.out.println("Fatal Error");
            System.exit(0);
            return "Error"; //to keep the compiler happy
}My question is that if I were to change the String month in the instance variables to type int, what changes do I make to the code to make this work WITHOUT changing the method headings and it also says that none of the type String paraenters should change to type int. I need to redefine the methods to make it work. I've been at this for hours and am just stuck so this is the reason for my early morning post.

import java.util.Scanner;
public class Date
    private int month;
    private int day;
    private int year; //a four digit number.
    public Date( )
        month = 1;
        day = 1;
        year = 1000;
    public Date(int monthInt, int day, int year)
        setDate(monthInt, day, year);
    public Date(String monthString, int day, int year)
        setDate(monthString, day, year);
    public Date(int year)
        setDate(1, 1, year);
    public Date(Date aDate)
        if (aDate == null)//Not a real date.
             System.out.println("Fatal Error.");
             System.exit(0);
        month = aDate.month;
        day = aDate.day;
        year = aDate.year;
    public void setDate(int monthInt, int day, int year)
        if (dateOK(monthInt, day, year))
            this.month = monthInt;
            this.day = day;
            this.year = year;
        else
            System.out.println("Fatal Error");
            System.exit(0);
    public void setDate(String monthString, int day, int year)
        if (dateOK(monthString, day, year))
            this.month = monthStringToInt(monthString);
            this.day = day;
            this.year = year;
        else
            System.out.println("Fatal Error");
            System.exit(0);
    public void setDate(int year)
        setDate(1, 1, year);
    public void setYear(int year)
        if ( (year < 1000) || (year > 9999) )
            System.out.println("Fatal Error");
            System.exit(0);
        else
            this.year = year;
    public void setMonth(int monthNumber)
        if ((monthNumber <= 0) || (monthNumber > 12))
            System.out.println("Fatal Error");
            System.exit(0);
        else
            month = monthNumber;
    public void setDay(int day)
        if ((day <= 0) || (day > 31))
            System.out.println("Fatal Error");
            System.exit(0);
        else
            this.day = day;
    public int getMonth( )
         return month;
    public int getDay( )
        return day;
    public int getYear( )
        return year;
    public String toString( )
        return (month + " " + day + ", " + year);
    public boolean equals(Date otherDate)
        return ( (month == otherDate.month)
                  && (day == otherDate.day) && (year == otherDate.year) );
    public boolean precedes(Date otherDate)
        return ( (year < otherDate.year) ||
           (year == otherDate.year && getMonth( ) < otherDate.getMonth( )) ||
           (year == otherDate.year && month == otherDate.month
                                         && day < otherDate.day) );
    public void readInput( )
        boolean tryAgain = true;
        Scanner keyboard = new Scanner(System.in);
        while (tryAgain)
            System.out.println("Enter month, day, and year.");
              System.out.println("Do not use a comma.");
            String monthInput = keyboard.next( );
            int dayInput = keyboard.nextInt( );
            int yearInput = keyboard.nextInt( );
            if (dateOK(monthInput, dayInput, yearInput) )
                setDate(monthInput, dayInput, yearInput);
                tryAgain = false;
            else
                System.out.println("Illegal date. Reenter input.");
    private boolean dateOK(int monthInt, int dayInt, int yearInt)
        return ( (monthInt >= 1) && (monthInt <= 12) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    private boolean dateOK(String monthString, int dayInt, int yearInt)
        return ( monthOK(monthString) &&
                 (dayInt >= 1) && (dayInt <= 31) &&
                 (yearInt >= 1000) && (yearInt <= 9999) );
    private boolean monthOK(String month)
        return (month.equals("January") || month.equals("February") ||
                month.equals("March") || month.equals("April") ||
                month.equals("May") || month.equals("June") ||
                month.equals("July") || month.equals("August") ||
                month.equals("September") || month.equals("October") ||
                month.equals("November") || month.equals("December") );
    private String monthString(int monthNumber)
        switch (monthNumber)
        case 1:
            return "January";
        case 2:
            return "February";
        case 3:
            return "March";
        case 4:
            return "April";
        case 5:
            return "May";
        case 6:
            return "June";
        case 7:
            return "July";
        case 8:
            return "August";
        case 9:
            return "September";
        case 10:
            return "October";
        case 11:
            return "November";
        case 12:
            return "December";
        default:
            System.out.println("Fatal Error");
            System.exit(0);
            return "Error"; //to keep the compiler happy
    private int monthStringToInt(String monthString) {
         if (monthString.equals("January")) return 1;
         else if (monthString.equals("February")) return 2;
         else if (monthString.equals("March")) return 3;
         else if (monthString.equals("April")) return 4;
         else if (monthString.equals("May")) return 5;
         else if (monthString.equals("June")) return 6;
         else if (monthString.equals("July")) return 7;
         else if (monthString.equals("August")) return 8;
         else if (monthString.equals("September")) return 9;
         else if (monthString.equals("October")) return 10;
         else if (monthString.equals("November")) return 11;
         else if (monthString.equals("December")) return 12;
         else return -1;
}I've added one method: monthStringToInt to convert "January", "February" into an integer value. There are probably better ways to do this (Calendar), but this involes no other classes.

Similar Messages

  • Easy Question -String conversion to type int

    Greetings Java Gurus,
    What is the best way to convert a String to type int?
    I have looked through the JAVA API documentation and have found the following possibilites but I cannot get the JAVA syntax right :(
    Java.lang.decode
    public static Integer decode(String nm)
    Decodes a String into an Integer.
    Java.lang.valueOf
    public static Integer valueOf(String s)
    Returns a new Integer object initialized to the value of the specified String.
    1.) If anyone has an easy way to accomplish this let me know.
    2.) If anyone could give me the proper JAVA syntax for the above commands?
    Any Ideas or Suggestions will be greatly appreciated!
    -Chibi_neko

    These are all methods of java.lang.Integer class.
    1. public static Integer decode( String nm )int a = -1;
    try {
        a = Integer.decode( "1234" );
    } catch( NumberFormatException nfe ) {
        a = -1;
    }2. public static int parseInt( String s )int a = -1;
    try {
        a = Integer.parseInt( "1234" );
    } catch( NumberFormatException nfe ) {
        a = -1;
    }3. public static int valueOf( String s )Integer a = null;
    try {
        a = Integer.valueOf( "1234" );
    } catch( NumberFormatException nfe ) {
        a = null;
    }

  • How to replace system fonts with type 1 version

    Can system font Helvetica be replaced with the PostScript Type 1 (Older) Version?
    Can a system font be deleted?
    System: iMac 2012, OS X 10.8.2, Output Device: PostScript Level 1 Imagesetter
    We are trying to pring Helvetica to a PostScript Level 1 device
    and do not want to convert system font to outlines in Illustrator in order to print
    to our output device as we would loose all editing capabilities.
    Our device will not accept TrueType or OpenType fonts only Type 1 fonts.
    Please explain if possible

    Same problem! The only solution I found is to prepare a textbox in a new file with all the font you need. Copy and past in the file you need to change font. Go to type/risolve missing font. Will open the same window in the pic you pasted. Then you find the font in the dropdown menu. I have to change all the Helvetica family ...it's a long go. If some one could save me. Hope to be usefull to your problem.

  • Replacement Ipod Dead With Same Problems, what am i doing wrong?

    I bought a Classic, it started to not play some of my songs, then it crashed, then i couldnt revive it or put anything on it, so i sent it back and received a new one promptly. Guess what, same problems, stopped playing some songs about a week ago, HDD makes strange sounds as if trying and failing to locate tracks, then i did a restore after i reworked my itunes library and now the pc is starting to not recognize it and cant add songs and restoring is a pointless option, ipod is full of songs up to the letter G after a failed library sync. PlEASE help me remedy so that i dont have to replace my replacement with another ipod that needs to be replaced

    Ok here's what I did:  While the modem was still connected to my PC, my router was plugged in and just had power so I did the hard reset like you said.  Then I plugged the modem into the router, reset my modem and plugged the ethernet cable back into my tower and Now it works.  The only problem is that I have is that when i get to http://198.162.1.1/ and leave username blank and password admin, I keep getting 401 Unauthorized.  I would really like to be on a secured network but I never could because the easylink advisor wouldn't work right and I couldn't get the laptop to get connected.  Thank you for your help.

  • Exit for replacing Calender month  with Calday

    Hi gurus,
    In my query  I have 2 key figures, key figure1 will work on Calday value entered by User . Key figure 2 is working on Calmonth value entered by User .Although the calmonth the user will have to enter will be the same one to which the calday would belong .
    To avoid the dupilcation and the chances of user entering mismatch day and month in the selection  I need to write an exit.Any help will be appreciated

    refer this doc:
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/25d98cf6-0d01-0010-0e9b-edcd4597335a

  • LCA is not an instance with type liveCache

    Dear collogues!
    According to "SAP NetWeaver 2004s SR1Standalone Engine  SAP liveCache Technology: UNIX" after installation of MaxDB aka LiveCache (from LifeCache CD)
    It should be done several post installation steps which also involve executing report
    "SLCA_NON_LC_RELEVANT_CLIENT" (in this report we must select productive client and choose "LiveCache relevant" radio button???) Ok. At last, step #14: initializing the liveCache instance LCA, which include Enter LCA in the Name of database connection field and Choose liveCache: Monitoring. When I push this button (LiveCache: Monitoring) system outputs "LCA is not an instance with type liveCache" What has been missed?
    Please help!

    Hi Ivan,
    while creating the instance using DBGUI its showing the error like :24785 the operating system user not a member of database administrator group.
    so plz help me to resolve this issue as soon as possible
    Thanks
    R@J

  • Grey screen no apple followed by blue screen with vertical lines. Graphics and logic board replaced 6 months ago. What to do? I held down shift, it gets HOT HOT and makes noises. (for safety mode) followed by a blank grey screen and blue screen

    grey screen no apple followed by blue screen with vertical lines. Graphics and logic board replaced <6 months ago. What to do? I held down shift, it gets HOT HOT and makes noises. (for safety mode) followed by a blank grey screen and blue screen. Then tried the other way (sorry, I think  it's command alt delete? I don't remember) and it turned from grey (no apple symbol) to blue blank. Then third time, grey followed by blue with vertical lines, not blocky clear lines that you can see if you are close to it.
    Thanks for any help!

    You have to take the computer to the Apple store to have it checked out.
    Genius Bar reservation
    http://www.apple.com/retail/geniusbar/
    Best.

  • Search and Replace String throwing the wrong error message with regexp?

    This came up in a LAVA thread, and I'm not sure if there's a bug here or not. When using Search and Replace string, and using a regular expression of [(G[b|i])], LabVIEW throws error -4622, "There is an unmatched parenthesis in a regular expression."  There are obviously no unmatched parenthesis in that expression, so it seems to me that the wrong error is being thrown. I'm just not sure if that's a syntactically valid regexp. The problem seems to be with nesting [ ]'s inside ( )'s inside [ ]'s. I've tried a couple of regexp resources on the Web, and one suggests it's valid, while the other seems to think it isn't.
    Message Edited by eaolson on 03-13-2007 10:33 AM
    Attachments:
    ATML_StandardUnit2.vi ‏10 KB
    regexp.png ‏5 KB

    adambrewster wrote:
    I think your regexp is invalid.
    In regexps, brackets are not the same as parentheses.  Parens are for grouping, while brackets are for matching one of a class of characters.  Brackets can not be nested.
    If the regexp is replaced with [G[bi]], there is no error, so it's not a matter of nested brackets. I couldn't find anything on the PCRE man page that forbids nested brackets specifically, but it makes sense.
    Your expression "[(G[bi])]", therefore parses as a character class which matches '(', 'G', '[', 'b', or 'i' followed by an unmatched paren, and an unmatched bracket.
    I don't believe that's the case. Replace the regexp with [(Gbi)], and the error goes away. So it's not a matter of the '(' being literal, and then encountering a ')' without a matching '('.
    daveTW wrote:
    what string exactly you want to replace? I think the round braces are not right in this case, since they mark partial matches which are given back by "match regular expression". But you don't want to extract parts of the string, you want to replace them (or delete, with empty <replace string>). So if you leave the outer [( ... )] then your RegEx means all strings with either "Gb" or "Gi".
    It's not my regular expression. A poster at LAVA was having problems with one of his (a truly frightening one), and this seemed to be the element that was causing the problem. I'm pretty sure that the originator of the regexp meant to use G(b|i), which seems like a complicated way of matching "Gb" or "Gi", if you ask me.

  • Replacing a char with string in a string buffer.

    Hi all,
    I have a huge xml file. Need to search for the following characters: ^ & | \ and replace each character with \T\ \F\ \B\ \C\ respectively.
    i.e. ^ must be replaced with \T\
    & must be replaced with \F\
    I see that it can be done in case of String using replaceAll() but have no clue about achieving using the stringbuffer??
    Any help is higly appeciated.
    Thanks in advance.

    Hi all,
    I have a huge xml file. Need to search for the
    following characters: ^ & | \ and replace each
    character with \T\ \F\ \B\ \C\ respectively.
    i.e. ^ must be replaced with \T\
    & must be replaced with \F\
    I see that it can be done in case of String using
    replaceAll() but have no clue about achieving using
    the stringbuffer??
    Any help is higly appeciated.
    Thanks in advance.here the solution for your problem:
    example code as follows,
         StringBuffer stringBuffer = new StringBuffer("your xml content");           
                String xmlString = stringBuffer.toString();
                char[]  xmlCharArray =  new char[];
                xmlString.getChars(0, xmlString.length()-1, xmlCharArray, 0);
               StringBuffer alteredXmlBuf = new StringBuffer();
                for(int i=0; i<xmlCharArray.length; i++){
                               if( xmlCharArray[i] == '^'){
                                      alteredXmlBuf.append(" \\T\\") ;
                               }else if(xmlCharArray[i] == '&'){
                                      alteredXmlBuf.append(" \\F\\") ;
                               }else if(xmlCharArray[i] == '|'){
                                      alteredXmlBuf.append(" \\B\\") ;
                               }else if(xmlCharArray[i] == '\'){
                                      alteredXmlBuf.append(" \\C\\") ;
                               }else {
                                       alteredXmlBuf.append(xmlCharArray);
    now alteredXmlBuf StringBuffer object contains your solution.

  • I have a work Mac, an air book, a home Mac; husband has desktop and laptop. That makes 5.  My work computer was replaced and I do not know what the company did with it.  I am not allowed to deauthorize all. Any way around the year limit?

    I have a work Mac, an air book, a home Mac; husband has desktop and laptop. That makes 5.  My work computer was replaced and I do not know what the company did with it.  I am not allowed to deauthorize all because I did this within the last year when I got my new airbook. Any way around the year limit?  I don't know if I can find my old computer and I don't want to wait months to be able to play my ituens purchases on my new work computer.

    If oyu already used up your "deauthorize all" option, you need to contact support so they can make an excemption:
    http://www.apple.com/support/itunes/contact/

  • ORA-02303: cannot drop or replace a type with type or table dependents

    Oracle 10.2.0.3 on solaris :
    I am trying to do a
    CREATE OR REPLACE TYPE WickType_StringArray AS TABLE OF VARCHAR2(256);
    I am getting the error :
    ORA-02303: cannot drop or replace a type with type or table dependents
    I then looked for the dependencies :
    select * from dba_dependencies where name = 'WICKTYPE_STRINGARRAY' and owner='HARPER';
    (Columns below with values are delimited by pipe | )
    OWNER|NAME|TYPE|REFERENCED_OWNER|REFERENCED_NAME|REFERENCED_TYPE|REFERENCED_LINK_NAME|DEPENDENCY_TYPE
    HARPER|WICKTYPE_STRINGARRAY|TYPE|SYS|STANDARD|PACKAGE||HARD     
    What is the best way for me to proceed to get my CREATE OR REPLACE statement working ?
    Thanks

    Well you could move to 11g but I'd suggest a less drastic measure for now. Save the data, drop the table, reload the table.
    But give serious consideration to Tom Kyte's advice about object tables: Don't use them. Instead use relational tables and object views.

  • I want replace my desktop with a iPad to do emails and Microsoft office work. Can this be effectively done? What's the downside if any?

    I want replace my desktop with a iPad to do emails and Microsoft office work. Can this be effectively done? What's the downside if any?

    I'll go down your list one by one.
    1. Access mail - which I know that the iPad can do.
    Enough said.
    2. Save an excel or word or ppt of PDF attachment for review.
    You can view these types of documents natively on the iPad as Tgara has mentioned.
    3. Open an review and maybe change a excel or word file.
    There are many apps that allow this.  I personally prefer Apples iWork programs, Pages, Numbers, Keynote.  They tightly integrate with iCloud.
    4. Resave such above files.
    With Pages, Number, Keynote you can save/resave files in iCloud and locally.  Also you could use dropbox to save your files on dropbox and provide a way for you to share a folder with coworkers/family and they can add/view/review files from there and you can see the changes on your device.
    5. Reply to a mail attaching a file.
    This is possible in many ways.  From dropbox, or another app you can email files with the share button.
    6. Be able to rename files and store them in specific folders.
    This goes back to dropbox/iCloud.  iOS doesn't really have a traditional file system.  If you save a PDF it isn't saved anywhere you can just open it.  You would have to open an app that supports view PDFs and select the PDF you wish to view.  The same goes for documents, spreadsheets and etc.
    Hopefully this helps answer your questions.

  • HT201250 I use Time Machine to back up my entire computer with my external hard drive. I am getting a brand new iMac this month and was wondering what is the process of using this back up to restore my new computer exactly how my old computer was?

    I use Time Machine to back up my entire computer with my external hard drive. I am getting a brand new iMac this month and was wondering what is the process of using this back up to restore my new computer exactly how my old computer was? I want to make sure I will still have various important files on my new computer, like my songs in iTunes, my photos in iPhoto, etc, etc. Thanks so much in advance!

    Welcome to the Apple Support Communities
    When you turn on the new iMac for the first time, Setup Assistant will ask you to restore a backup, so connect the external disk and follow steps to restore all your files to your new iMac. Your new Mac will have the same settings and programs as your old computer.
    In other cases, I would recommend to restore the whole backup without using Migration Assistant or Setup Assistant, but a Late 2012 iMac uses a special OS X build, so the OS X version that you're using on your old Mac won't work on the new one. For more information, see > http://pondini.org/OSX/Home.html

  • I replaced my iPhone with a Galaxy 5 and am not receiving all of my text messages. The sales person told me to remove my phone number from iTunes to stop this from happening. Is that true? What does iTunes have to do with text messages?

    I replaced my iPhone with a Samsung Galaxy5 and I am not receiving all of my text messages now. The rep at the store told me to go to iTunes and take my phone # off of the account and that would fix the problem. Is that true? What does iTunes have to do with text messages anyway?

    You need to remove your phone number from the iMessage system.
    Read here: http://support.apple.com/kb/ts5185
    ~Lyssa

  • My one year warranty expired recently, a month ago my charger was replaced under warranty and it has only lasted a month and has broken again, does the replacement charger come with a new warranty or will i have to buy a new one?

    Bought the phone on contract last January (late in january), had my charger replaced just after christmas and now it has stopped working again, its been about a month and a half and the replacement charger is broken, does the replacement charger come with a new warranty or will I have to buy a new charger? And if so, is there a charger that doesn't break easily (my problem is the the wire at the phone end of the cable seems to bunch and twist etc.)
    thanks.

    a warranty replacement if the donor is out of warranty has a new 90 days warranty from exchange date 

Maybe you are looking for