I'm sure there's a really easy explanation to this, but I've been programming
all day (or trying to) and by now, my mind has started to melt! I'm using
BlueJ btw!
I'm trying to display available slots in a day, which come as half hour
periods.
The code below takes the start of the day as being an integer (9) and the end
of the day as an integer also (17). It should then split the day up into 16
sections and output them to the terminal as:
09:00
09:30
10:00
...
16:30
To do this, I have set up two strings, one for hours (hour!) and one for
minutes (minute!). These strings are formed by two integers, one is limit,
which is used as the limit for the loop as well as the hour, and the other is
min, incremented in batches of 30, then reset to 0 instead of reaching 60.
When "min" is "0", "limit" is incremented.
To make it into 24 hour format, if "limit" is < 10, an extra 0 is added to
the string "hour" and if "min" is 0, an extra 0 is added to the string
"minute".
If someone could tell me why this is not outputting to the terminal it would
really help.
public void showAppointments()
{
String hour = ("");
int min = 0;
String minute = ("");
for(int limit = 9; limit > 17; ) {
hour = ("");
minute = ("");
if ((limit - 10) < 0) {
hour = ("0" + limit);
}
else {
hour = ("" + limit);
}
if (min == 0) {
minute = ("0" + min);
}
else {
minute = ("" + min);
}
System.out.println(hour + ":" + minute);
min = min + 30;
if (min == 60) {
min = 0;
}
if (min == 0) {
limit++;
}
}
}
Patricia Shanahan - 30 Jul 2005 18:42 GMT
> I'm sure there's a really easy explanation to this, but I've been programming
> all day (or trying to) and by now, my mind has started to melt! I'm using
> BlueJ btw!
...
> If someone could tell me why this is not outputting to the terminal it would
> really help.
[quoted text clipped - 5 lines]
> String minute = ("");
> for(int limit = 9; limit > 17; ) {
The condition for continuing in the loop is backwards. At the start of
the first iteration, you will drop out of the loop because 9 is not
greater than 17.
When you get stuck on something like this, get rid of all the code that
is not needed to reproduce the problem. When you got it down to:
public static void main(String[] args){
for(int limit = 9; limit > 17 ; ){
System.out.println("GOT HERE!");
limit++;
}
}
the problem would have been obvious even with a melted mind.
Patricia
makzan - 30 Jul 2005 19:16 GMT
Thanks Patricia.
It's working fine now. I'm going to pick it up again tomorrow though and have
a rest now!!!!!