Good evening, all!

My current assignment is a small Appointment Book written in Java. It should scroll through the times of the day, and display the appointment (if any) for that particular time of day.

I can scroll through the different times with no issue, but I can't seem to get the appointment(s) to display correctly. Any appointment I put in is displayed for EVERY hour, not just the one it is intended for.

This is a portion from my main.java file:

The saveButton saves the appointment
nameField is the name of the text field holding the appointment

if (e.getSource()==this.saveButton){
            String temptext = (nameField.getText());
            appointments.setName(temptext);
            nameField.setText(appointments.getName());
}

And this portion is the part of my constructor file that is giving me the issue:

public void setName(String appt){
        setName(appt);
    }
    public String getName(){
        return appt;
    }

Any help on what I'm missing would be greatly appreciated!

Can you see something wrong with this code:

public void setName(String appt){
        [B]setName[/B](appt);
    }

You calling the same method. When you call setName it will call setName which is inside it, which will call setName which is inside it, which will call setName which is inside it, . . . .

You need to assign the property name inside the method:

public void setName(String appt){
        name = appt;
    }
commented: Thank you! +1

Progress! Your fix certainly eliminated the repeated error I was receiving (totally missed that - tyvm!!)

However the same appointment is still stored at every time segment throughout the book.

Post more code. Skip the presentation code and post for start just the code with the logic.

I'm hope I am correct in assuming this is what you are looking for. Again, thank you for taking the time to look at this.

This is from the constructor file:

package AppointmentBook;

import javax.swing.Icon;
import javax.swing.JOptionPane;

/**
 *
 * @author Jim
 */
public class AppointmentBook {

    private int hour[];
    private int time, position = 0;
    private int max;
    private String[] text;
    private String result;
    private String appt;
    private String temptext;

    public AppointmentBook(int start, int end) {
        int j = start;
        int[] timeArray;                //Array for appointment times
        String[] stringArray;           //Array for appointments

        int temp = end - start;         //Specified time period
        temp = temp/100;                
        temp++;

        timeArray = new int[temp];      //Space for arrays
        stringArray = new String[temp];

        for (int i=0;i<temp;i++) {      //Default values assigned
            stringArray[i] = "none";    
            timeArray[i] = j;
            j += 100;
        }

        time = 0;                       //Update state variables
        max = temp - 1;                 //All these are defined globally
        hour = timeArray;  
        text = stringArray;  


    }
    public String getTime(){
        time = hour[position];             //time at that spot in the array
        String result = "";                //the time we're on
        result = Integer.toString(time);   //change Int to String
        return result;                     //return value
    }
    public void advPosition(boolean b){    //Instead of void next() and void prev()
                                           //I used one function to do both
        if (b)
        {
            position++;                    //cycle through the array
            if (position > max)            //If 1500 is the time then...
            {
                position=max;              //Time stops cycling forward through the hours
            }

        }
        else
        {
            position--;
            if (position < 0)               //If 800 is the time then...
            {
                position=0;                 //Time stops cycling backward through the hours
            }
        }
 
        
    }
  
    public void setName(String appt){
        temptext = appt;
    }
    public String getName(){
        return appt;
    }

}

And this is from the Main.java file:

public void actionPerformed(ActionEvent e) {


        if (e.getSource()==this.nextButton){
            appointments.advPosition(true);
            timeField.setText(appointments.getTime());
        }
        if (e.getSource()==this.prevButton){
            appointments.advPosition(false);
            timeField.setText(appointments.getTime());
        }
        if (e.getSource()==this.saveButton){
            String temptext = (nameField.getText());
            appointments.setName(temptext);
            //nameField.setText(appointments.getName());
        }


    }

First of all look at this again:

public void setName(String appt){
        temptext = appt;
    }
    public String getName(){
        return appt;
    }

You return the appt variable but in the code that variable never takes value. You just declare it and it always returns null. But you set the value to the temptext variable; shouldn't you return that instead?
Also these 2 appt variables are NOT the same:

private Strign appt = null;

    public void setName(String appt){ // appt exists only inside this method
// it overrides the global
       temptext = appt;
    }

When you call the set-method the global variable appt doesn't take value.

And a new suggestion if you can't get you code to work.
I find your solution very confusing and not object oriented. I believe that you problem is that when it comes to time you return values from the array, but when it comes to name you declare an array but you use a single String variable which you set and return. Therefor all the times will have the same name from the method "getName".
Shouldn't you have a method that depending on the time or its position you set the at the appropriate position the name at the StringArray?

I don't know your whole logic but you can try this:

// SINGLE CLASS WITH JUST PROPERTIES

public class Appointment {
  private int time = 0;
  private String name = "";

  public Appointment() {
 
  }

// add get,set methods

}

Then have a class like this:

public class AppointmentBook {
  private int position = 0;
  private int max = 0;
  private Appointment [] appointments = null;

  public Appointment(int max) {
     // this.max is the global
    // max is the local from the argument
     this.max = max;

    //appointments is the global
    appointments = new Appointment[max];
  }
  
   // create methods that add and get appointments to the array by increasing the position variable
  // also check the value of position in case you have too much appointment
  // add whatever functionality you want
}

examle:

Appointment ap = new Appointment();
ap.setName("Name");
ap.setTime(200);

Appointment [] appointS = new Appointment[5];
appointS[0] = ap;

// OR

appointS[1] = new Appointment();
appointS[1].setName("Name 2");
appointS[1].setTime(300);

Remember when you declare an array its elements are undeclared.
This: Appointment [] appointS = new Appointment[5]; will create an array but its elements (appointS[0], ... ) are null. You need to give them values like at the example

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.