943,815 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1199
  • Java RSS
Sep 25th, 2009
0

Java Appointment Book

Expand Post »
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

Java Syntax (Toggle Plain Text)
  1. if (e.getSource()==this.saveButton){
  2. String temptext = (nameField.getText());
  3. appointments.setName(temptext);
  4. nameField.setText(appointments.getName());
  5. }

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

Java Syntax (Toggle Plain Text)
  1. public void setName(String appt){
  2. setName(appt);
  3. }
  4. public String getName(){
  5. return appt;
  6. }

Any help on what I'm missing would be greatly appreciated!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
JimD C++ Newb is offline Offline
46 posts
since Oct 2008
Sep 25th, 2009
1

Re: Java Appointment Book

Can you see something wrong with this code:

public void setName(String appt){
        setName(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:
Java Syntax (Toggle Plain Text)
  1. public void setName(String appt){
  2. name = appt;
  3. }
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 25th, 2009
0

Re: Java Appointment Book

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
JimD C++ Newb is offline Offline
46 posts
since Oct 2008
Sep 25th, 2009
0

Re: Java Appointment Book

Post more code. Skip the presentation code and post for start just the code with the logic.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 25th, 2009
0

Re: Java Appointment Book

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:

Java Syntax (Toggle Plain Text)
  1. package AppointmentBook;
  2.  
  3. import javax.swing.Icon;
  4. import javax.swing.JOptionPane;
  5.  
  6. /**
  7.  *
  8.  * @author Jim
  9.  */
  10. public class AppointmentBook {
  11.  
  12. private int hour[];
  13. private int time, position = 0;
  14. private int max;
  15. private String[] text;
  16. private String result;
  17. private String appt;
  18. private String temptext;
  19.  
  20. public AppointmentBook(int start, int end) {
  21. int j = start;
  22. int[] timeArray; //Array for appointment times
  23. String[] stringArray; //Array for appointments
  24.  
  25. int temp = end - start; //Specified time period
  26. temp = temp/100;
  27. temp++;
  28.  
  29. timeArray = new int[temp]; //Space for arrays
  30. stringArray = new String[temp];
  31.  
  32. for (int i=0;i<temp;i++) { //Default values assigned
  33. stringArray[i] = "none";
  34. timeArray[i] = j;
  35. j += 100;
  36. }
  37.  
  38. time = 0; //Update state variables
  39. max = temp - 1; //All these are defined globally
  40. hour = timeArray;
  41. text = stringArray;
  42.  
  43.  
  44. }
  45. public String getTime(){
  46. time = hour[position]; //time at that spot in the array
  47. String result = ""; //the time we're on
  48. result = Integer.toString(time); //change Int to String
  49. return result; //return value
  50. }
  51. public void advPosition(boolean b){ //Instead of void next() and void prev()
  52. //I used one function to do both
  53. if (b)
  54. {
  55. position++; //cycle through the array
  56. if (position > max) //If 1500 is the time then...
  57. {
  58. position=max; //Time stops cycling forward through the hours
  59. }
  60.  
  61. }
  62. else
  63. {
  64. position--;
  65. if (position < 0) //If 800 is the time then...
  66. {
  67. position=0; //Time stops cycling backward through the hours
  68. }
  69. }
  70.  
  71.  
  72. }
  73.  
  74. public void setName(String appt){
  75. temptext = appt;
  76. }
  77. public String getName(){
  78. return appt;
  79. }
  80.  
  81. }

And this is from the Main.java file:

Java Syntax (Toggle Plain Text)
  1.  
  2. public void actionPerformed(ActionEvent e) {
  3.  
  4.  
  5. if (e.getSource()==this.nextButton){
  6. appointments.advPosition(true);
  7. timeField.setText(appointments.getTime());
  8. }
  9. if (e.getSource()==this.prevButton){
  10. appointments.advPosition(false);
  11. timeField.setText(appointments.getTime());
  12. }
  13. if (e.getSource()==this.saveButton){
  14. String temptext = (nameField.getText());
  15. appointments.setName(temptext);
  16. //nameField.setText(appointments.getName());
  17. }
  18.  
  19.  
  20. }
Reputation Points: 10
Solved Threads: 0
Light Poster
JimD C++ Newb is offline Offline
46 posts
since Oct 2008
Sep 25th, 2009
0

Re: Java Appointment Book

First of all look at this again:
Java Syntax (Toggle Plain Text)
  1. public void setName(String appt){
  2. temptext = appt;
  3. }
  4. public String getName(){
  5. return appt;
  6. }

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:
Java Syntax (Toggle Plain Text)
  1. private Strign appt = null;
  2.  
  3. public void setName(String appt){ // appt exists only inside this method
  4. // it overrides the global
  5. temptext = appt;
  6. }
When you call the set-method the global variable appt doesn't take value.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Sep 25th, 2009
0

Re: Java Appointment Book

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:
Java Syntax (Toggle Plain Text)
  1. // SINGLE CLASS WITH JUST PROPERTIES
  2.  
  3. public class Appointment {
  4. private int time = 0;
  5. private String name = "";
  6.  
  7. public Appointment() {
  8.  
  9. }
  10.  
  11. // add get,set methods
  12.  
  13. }

Then have a class like this:
Java Syntax (Toggle Plain Text)
  1. public class AppointmentBook {
  2. private int position = 0;
  3. private int max = 0;
  4. private Appointment [] appointments = null;
  5.  
  6. public Appointment(int max) {
  7. // this.max is the global
  8. // max is the local from the argument
  9. this.max = max;
  10.  
  11. //appointments is the global
  12. appointments = new Appointment[max];
  13. }
  14.  
  15. // create methods that add and get appointments to the array by increasing the position variable
  16. // also check the value of position in case you have too much appointment
  17. // add whatever functionality you want
  18. }

examle:
Java Syntax (Toggle Plain Text)
  1. Appointment ap = new Appointment();
  2. ap.setName("Name");
  3. ap.setTime(200);
  4.  
  5. Appointment [] appointS = new Appointment[5];
  6. appointS[0] = ap;
  7.  
  8. // OR
  9.  
  10. appointS[1] = new Appointment();
  11. appointS[1].setName("Name 2");
  12. 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
Last edited by javaAddict; Sep 25th, 2009 at 5:57 am.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Spring bean config problem
Next Thread in Java Forum Timeline: Memory Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC