-4

I've been working on this for hours, and while this is due tonight I got my wisdom teeth out today and the anesthesia is making me really easily confused.

I need is two methods within the class, "toString", which takes dd/mm/yyyy and prints that, as well as "advance" which modifies the day + 1. When I check the modified date, I receive this:

Initial date: 88/8/8888

Modified date: 88/0/8888




    int day, month, year, newDay;
    String decision, dummy ;        
    Scanner read = new Scanner(System.in);

    public static void main(String[] args) {
        Date dateInstance = new Date();
        dateInstance.toString();
        dateInstance.advance();
    }

    public String toString() {
        System.out.println("Enter day (mm/xx/yyyy): ");
        day = read.nextInt();
        System.out.println("Enter month (xx/dd/yyyy): ");
        month = read.nextInt();
        System.out.println("Enter year (mm/dd/xxxx): ");
        year = read.nextInt();

        System.out.println("Initial date: "+month+"/"+day+"/"+year);
        System.out.println("Modified date: "+month+"/"+newDay+"/"+year);

        return null;

        /*
        String decision = read.nextLine();
        System.out.println("Would you like to display the date, and the modified date? (Y / N): ");

        if(decision == "N") {
            System.out.println("'N' Selected");
        }else if(decision == "Y") {
            System.out.println("Initial date: "+month+"/"+day+"/"+year);
            System.out.println("Modified date: "+month+"/"+newDay+"/"+year);
        }
        return dummy;
        */
    }

    public int advance() {
        newDay = day + 1;
        return newDay;
    }
  • 5
    I have to wonder if you're misunderstanding your requirements and traveling down the wrong path. For example, a `toString()` method should look nothing like that, and should never interact with the user as your code is doing. – Hovercraft Full Of Eels Jun 29 '17 at 23:28
  • 3
    Perhaps best to talk to your teacher, tell them about your anesthesia issues, and get an extension as well as clarification and help with this assignment. – Hovercraft Full Of Eels Jun 29 '17 at 23:29
  • Call `advance()` **before** you call `toString()`. As is, you call `toString` and `newDay` is `0` because you haven't called `advance()` to set it yet. Also, do your homework *before* surgery. – Elliott Frisch Jun 29 '17 at 23:29
  • I'll review toString();, and thank you for the last reply Elliot! I got most of it done last night but didn't' get it finished. Dully noted :) – Corey Maroney Jun 29 '17 at 23:31
  • 1
    Search Stack Overflow before posting. Both your issues have been covered many many times already. – Basil Bourque Jun 30 '17 at 00:22

1 Answers1

0

Here is some code to get you started. I would guess that your teacher doesn't want you to rewrite the Date's toString() method but actually use it to create your own function that displays the output in another format (Hint - opposite).

    Date dateInstance = new Date(System.currentTimeMillis());
    Date dateForward = new Date(dateInstance.getTime() + 1000*60*60*24); //put this in a method
    System.out.println(dateInstance.toString()); //use the split method to extract and rearrange the date
    System.out.println(dateForward.toString());

Check out the Java 8 Docs for the Date Object

Output

2017-06-29
2017-06-30
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 30 '17 at 00:24
  • 1
    No need to do your own math like `1000*60*60*24`. Doing so is error-prone, and difficult to read. Regarding errors, you are ignoring the crucial issue of time zone. We have classes designed expressly for this work. `LocalDate.now( ZoneId.of( "America/Montreal" ) ).plusDays( 1 )` – Basil Bourque Jun 30 '17 at 00:26