PDA

View Full Version : How do I make little exceptional spaces with for loop?



s3a
October 11th, 2009, 02:08 AM
Here is what I am supposed to get:


Day of Leap Year Calendar
---------------------------------------------------------------------------
Day of Day of
Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Month
--------------------------------------------------------------------------
1 1 32 61 92 122 153 183 214 245 275 306 336 1
2 2 33 62 93 123 154 184 215 246 276 307 337 2
3 3 34 63 94 124 155 185 216 247 277 308 338 3
4 4 35 64 95 125 156 186 217 248 278 309 339 4
5 5 36 65 96 126 157 187 218 249 279 310 340 5
6 6 37 66 97 127 158 188 219 250 280 311 341 6
7 7 38 67 98 128 159 189 220 251 281 312 342 7
8 8 39 68 99 129 160 190 221 252 282 313 343 8
9 9 40 69 100 130 161 191 222 253 283 314 344 9
10 10 41 70 101 131 162 192 223 254 284 315 345 10
11 11 42 71 102 132 163 193 224 255 285 316 346 11
12 12 43 72 103 133 164 194 225 256 286 317 347 12
13 13 44 73 104 134 165 195 226 257 287 318 348 13
14 14 45 74 105 135 166 196 227 258 288 319 349 14
15 15 46 75 106 136 167 197 228 259 289 320 350 15
16 16 47 76 107 137 168 198 229 260 290 321 351 16
17 17 48 77 108 138 169 199 230 261 291 322 352 17
18 18 49 78 109 139 170 200 231 262 292 323 353 18
19 19 50 79 110 140 171 201 232 263 293 324 354 19
20 20 51 80 111 141 172 202 233 264 294 325 355 20
21 21 52 81 112 142 173 203 234 265 295 326 356 21
22 22 53 82 113 143 174 204 235 266 296 327 357 22
23 23 54 83 114 144 175 205 236 267 297 328 358 23
24 24 55 84 115 145 176 206 237 268 298 329 359 24
25 25 56 85 116 146 177 207 238 269 299 330 360 25
26 26 57 86 117 147 178 208 239 270 300 331 361 26
27 27 58 87 118 148 179 209 240 271 301 332 362 27
28 28 59 88 119 149 180 210 241 272 302 333 363 28
29 29 60 89 120 150 181 211 242 273 303 334 364 29
30 30 90 121 151 182 212 243 274 304 335 365 30
31 31 91 152 213 244 305 366 31

AND

Day of Non-Leap Year Calendar
---------------------------------------------------------------------------
Day of Day of
Month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Month
--------------------------------------------------------------------------
1 1 32 60 91 121 152 182 213 244 274 305 335 1
2 2 33 61 92 122 153 183 214 245 275 306 336 2
3 3 34 62 93 123 154 184 215 246 276 307 337 3
4 4 35 63 94 124 155 185 216 247 277 308 338 4
5 5 36 64 95 125 156 186 217 248 278 309 339 5
6 6 37 65 96 126 157 187 218 249 279 310 340 6
7 7 38 66 97 127 158 188 219 250 280 311 341 7
8 8 39 67 98 128 159 189 220 251 281 312 342 8
9 9 40 68 99 129 160 190 221 252 282 313 343 9
10 10 41 69 100 130 161 191 222 253 283 314 344 10
11 11 42 70 101 131 162 192 223 254 284 315 345 11
12 12 43 71 102 132 163 193 224 255 285 316 346 12
13 13 44 72 103 133 164 194 225 256 286 317 347 13
14 14 45 73 104 134 165 195 226 257 287 318 348 14
15 15 46 74 105 135 166 196 227 258 288 319 349 15
16 16 47 75 106 136 167 197 228 259 289 320 350 16
17 17 48 76 107 137 168 198 229 260 290 321 351 17
18 18 49 77 108 138 169 199 230 261 291 322 352 18
19 19 50 78 109 139 170 200 231 262 292 323 353 19
20 20 51 79 110 140 171 201 232 263 293 324 354 20
21 21 52 80 111 141 172 202 233 264 294 325 355 21
22 22 53 81 112 142 173 203 234 265 295 326 356 22
23 23 54 82 113 143 174 204 235 266 296 327 357 23
24 24 55 83 114 144 175 205 236 267 297 328 358 24
25 25 56 84 115 145 176 206 237 268 298 329 359 25
26 26 57 85 116 146 177 207 238 269 299 330 360 26
27 27 58 86 117 147 178 208 239 270 300 331 361 27
28 28 59 87 118 148 179 209 240 271 301 332 362 28
29 29 88 119 149 180 210 241 272 302 333 363 29
30 30 89 120 150 181 211 242 273 303 334 364 30
31 31 90 151 212 243 304 365 31

But I don't know how to make those empty spaces on the few exceptional columns.
Here is my source code:


public class Q

{

public static void main (String [] args)

{



System.out.println("Day of\nmonth");





// For a LEAP year

for(int day = 1; day <= 31; day++)

{

final int JANUARY = 31;

final int FEBRUARY_LEAP = 29;

final int MARCH = 31;

final int APRIL = 30;

final int MAY = 31;

final int JUNE = 30;

final int JULY = 31;

final int AUGUST = 31;

final int SEPTEMBER = 30;

final int OCTOBER = 31;

final int NOVEMBER = 30;

final int DECEMBER = 31;



int january = day;

int february = january + JANUARY;

int march = february + FEBRUARY_LEAP;

int april = march + MARCH;

int may = april + APRIL;

int june = may + MAY;

int july = june + JUNE;

int august = july + JULY;

int september = august + AUGUST;

int october = september + SEPTEMBER;

int november = october + OCTOBER;

int december = november + NOVEMBER;



System.out.printf("%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d "

, day , january, february, march, april, may, june, july, august, september, october, november, december, day);

System.out.println();

}

System.out.println();



// For a non-LEAP year

for(int day = 1; day <= 31; day++)

{

final int JANUARY = 31;

final int FEBRUARY_NON_LEAP = 28;

final int MARCH = 31;

final int APRIL = 30;

final int MAY = 31;

final int JUNE = 30;

final int JULY = 31;

final int AUGUST = 31;

final int SEPTEMBER = 30;

final int OCTOBER = 31;

final int NOVEMBER = 30;

final int DECEMBER = 31;



int january = day;

int february = january + JANUARY;

int march = february + FEBRUARY_NON_LEAP;

int april = march + MARCH;

int may = april + APRIL;

int june = may + MAY;

int july = june + JUNE;

int august = july + JULY;

int september = august + AUGUST;

int october = september + SEPTEMBER;

int november = october + OCTOBER;

int december = november + NOVEMBER;



System.out.printf("%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d "

, day , january, february, march, april, may, june, july, august, september, october, november, december, day);

System.out.println();

}



}

}


Any help would be greatly appreciated!
Thanks in advance!

jpkotta
October 11th, 2009, 08:26 AM
I don't know Java, but pretty much any language supports C-style formatted printing. So assuming that's the case, try '% 4d'. Read the documentation on println().

geirha
October 11th, 2009, 12:15 PM
See String.format() and PrintStream.printf() (System.out is an instance of PrintStream)

EDIT: Err, sorry. You're already using printf, my bad for not reading properly.