Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't my main class see the array in my calender class
    text
    copied!<p>This is a homework problem. I'm already 5 days late and can't figure out what I'm doing wrong.. this is my 1st semester in Java and my first post on this site</p> <p>Here is the assignment..</p> <p>Create a class called <code>Calendar</code>. The class should contain a variable called <code>events</code> that is a String array. The array should be created to hold 5 elements. Use a constant value to specify the array size. Do not hard code the array size. Initialize the array in the class constructor so that each element contains the string <strong>“ – No event planned – “</strong>. </p> <p>The class should contain a method called <code>CreateEvent</code>. This method should accept a String argument that contains a one-word user event and an integer argument that represents the day of the week. <code>Monday</code> should be represented by the number <code>1</code> and <code>Friday</code> should be represented by the number <code>5</code>. Populate the events array with the event info passed into the method. Although the user will input one-word events, each event string should prepend the following string to each event:</p> <pre><code>event_dayAppoinment: (where event_day is the day of the week) </code></pre> <p>For example, if the user enters 1 and “doctor” , the first array element should read: Monday Appointment: doctor</p> <p>If the user enters 2 and “PTA” , the second array element should read: Tuesday Appointment: PTA</p> <p>Write a driver program (in a separate class) that creates and calls your Calendar class. Then use a loop to gather user input. Ask for the day (as an integer) and then ask for the event (as a one word string). Pass the integer and string to the Calendar object’s <code>CreateEvent</code> method. The user should be able enter 0 – 5 events. If the user enters <code>-1</code>, the loop should exit and your application should print out all the events in a tabular format. Your program should not allow the user to enter invalid values for the day of the week. Any input other than <code>1 – 5</code> or <code>-1</code> for the day of the week would be considered invalid.</p> <p>Notes: When obtaining an integer from the user, you will need to use the <code>nextInt()</code> method on your Scanner object. When obtaining a string from a user, you will need to use the <code>next()</code> method on your Scanner object.</p> <p>Here is my code so far..</p> <pre><code>//DRIVER CLASS /** * * @author Rocky */ //imports scanner import java.util.Scanner; //begin class driver public class driver { /** * @paramargs the command line arguments */ //begin main method public static void main(String[] args) { //initiates scanner Scanner userInput = new Scanner (System.in); //declare variables int dayOfWeek; String userEvent; //creates object for calender class calendercalenderObject = new calender(); //user prompt System.out.println("Enter day of week for your event in the following format:"); System.out.println("Enter 1 for Monday"); System.out.println("Enter 2 for Tuesday"); System.out.println("Enter 3 for Wednsday"); System.out.println("Enter 4 for Thursday"); System.out.println("Enter 5 for Friday"); System.out.println("Enter -1 to quit"); //collect user input dayOfWeek = userInput.nextInt(); //user prompt System.out.println("Please type in the name of your event"); //collect user input userEvent = userInput.next(); //begin while loop while (dayOfWeek != -1) { //test for valid day of week if ((dayOfWeek&gt;=1) &amp;&amp; (dayOfWeek&lt;=5)){ //calls createEvent method in calender class and passes 2 variables calenderObject.createEvent(userEvent,dayOfWeek); } else { //error message System.out.println("You have entered an invalid number"); //user prompts System.out.println("Press -1 to quit or enter another day"); System.out.println("Enter 1 for Monday"); System.out.println("Enter 2 for Tuesday"); System.out.println("Enter 3 for Wednsday"); System.out.println("Enter 4 for Thursday"); System.out.println("Enter 5 for Friday"); System.out.println("Enter -1 to quit"); //collect user input dayOfWeek = userInput.nextInt(); //end data validity test } //end while loop } //prints array to screen int i=0; for (i=0;i&lt;events.length;i++){ System.out.println(events[i]); } //end main method } } /** * * @author Rocky */ //imports scanner import java.util.Scanner; //begin calender class public class calender { //creates events array String[] events = new String[5]; //begin calender class constructor public calender() { //Initializes array String[] events = {"-No event planned-","-No event planned-","-No event planned-","-No event planned-","-No event planned-"}; //end calender class constructor } //begin createEvent method public String[] createEvent (String userEvent, int dayOfWeek){ //Start switch test switch (dayOfWeek){ case 1: events[0] = ("Monday Appoinment:") + userEvent; break; case 2: events[1] = ("Tuesday Appoinment:") + userEvent; break; case 3: events[2] = ("WednsdayAppoinment:") + userEvent; break; case 4: events[3] = ("Thursday Appoinment:") + userEvent; break; case 5: events[4] = ("Friday Appoinment:") + userEvent; break; default: break; //End switch test } //returns events array return events; //end create event method } //end calender class } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload