| | |
Java Appointment Book
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
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
And this portion is the part of my constructor file that is giving me the issue:
Any help on what I'm missing would be greatly appreciated!
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)
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:
Java Syntax (Toggle Plain Text)
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:
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){
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)
public void setName(String appt){ name = appt; }
Check out my New Bike at my Public Profile at the "About Me" tab
Post more code. Skip the presentation code and post for start just the code with the logic.
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
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:
And this is from the Main.java file:
This is from the constructor file:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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:
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:
When you call the set-method the global variable appt doesn't take value.
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
private Strign appt = null; public void setName(String appt){ // appt exists only inside this method // it overrides the global temptext = appt; }
Check out my New Bike at my Public Profile at the "About Me" tab
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:
Then have a class like this:
examle:
Remember when you declare an array its elements are undeclared.
This:
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)
// 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:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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 Last edited by javaAddict; Sep 25th, 2009 at 5:57 am.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Is this book <JAVA in a netshell> good for beginners? (Java)
- One Month to learn Java - Pointers and book suggestions needed (Java)
- Learning Java (Java)
- Help tweaking some java (JavaScript / DHTML / AJAX)
- New to Netbeans and Slightly to Java (Java)
- Place to start learning Java! (Java)
- Favorite Java textbook (Java)
- Java editor (Java)
- Beginning Programming with Java (Java)
Other Threads in the Java Forum
- Previous Thread: Spring bean config problem
- Next Thread: Memory Problems
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






