1

My code compiles and runs how I want it to, however, I have a question, after I am done adding events in the calendar, unless there was a specific event for that day, the result that comes to terminal shows that for each day there is nothing the result is 'null' I was wondering if there was any way I could change that

public class Calendar {
public static void main(String[] args) {

    String[] calender = new String[32];
    String ans = " ";

    do {
        System.out.print("Type the number day of your event: ");
        int date = IO.readInt();
        System.out.println("What is the event?");
        String event = IO.readString();
        System.out.println("Do you have another event to add? y/n: ");
        addEvent(calender, event, date);
        ans = IO.readString();
    } while (ans.equals("y"));

    printCal(calender);


}

public static void addEvent(String[] calender, String event, int date) {

    calender[date] = event;

    // return true;

}

public static void printCal(String[] calender) {
    for (int i=1;i<calender.length;i++) {
        System.out.println("Day: " + i + " " + " Event: " + calender[i]);
    }
}

}

Terminal Result

4
  • If you don't want the value to be null, put something else in the array. If you just don't want to print null, put a check in to print the default value of your choice if the value at that index is null. Commented Jul 6, 2015 at 20:41
  • (calender[i] == null ? "placeholder" : calender[i]) Commented Jul 6, 2015 at 20:43
  • Sorry, should have referenced, IO is a predefined class that my school has given us to handle input and output, does the same thing as a Scanner, but when I'm making practice programs its easier to utilize that rather than establishing a Scanner. Commented Jul 6, 2015 at 20:44
  • Do you need to use array? Commented Jul 6, 2015 at 20:45

2 Answers 2

3

While printing you can check if it is null and if it is, print something else. In this case, it will print "Holding Text" instead of null. Feel free to change it to whatever you want.

public static void printCal(String[] calender) {
    for (int i=1;i<calender.length;i++) {
        String event = (calendar[i] == null) ? "Holding Text" : calendar[i];
        System.out.println("Day: " + i + " " + " Event: " + event);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, this performed exactly how I wanted!
Glad I could help. You should mark as solved if your question is answered.
1

If you're using java8 you can employ Optional class to set the placeholder:

public static void addEvent(String[] calender, String event, int date) {

    calender[date] = Optional.ofNullable(event).orElse("placeholder");

    // return true;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.