Java Quiz #42 : A string too far

(FR)
Ce code semble totalement inoffensif, mais peut causer des erreurs sous certaines circonstances.
Pouvez-vous détecter le problème ?

(EN)
This code may seem harmless, but will cause serious problems in certain circumstances.
Can you spot the problem ?

  1. public class Quiz42 {
  2.  
  3. public static void main(String[] args) {
  4. new Quiz42();
  5. }
  6.  
  7. private static final int MAX_STRING_LENGTH = 6;
  8. private Map<Integer, String> db = new HashMap<Integer, String>();
  9. private int nextId = 0;
  10.  
  11. public Quiz42() {
  12. // Introductory text
  13. Console console = System.console();
  14. if (console==null) {
  15. System.out.println("Don't cheat in Eclipse :)");
  16. System.exit(0);
  17. }
  18. console.printf(
  19. "FakeDB 1.0%n"+
  20. "'exit': exits FakeDB.%n" +
  21. "'db' : dumps the DB content.%n"+
  22. "Words are limited to %d characters.%n%n> ",MAX_STRING_LENGTH);
  23.  
  24. // Read - Eval loop
  25. String s = null;
  26. while ((s = console.readLine()) != null) {
  27. if ("exit".equals(s)) {
  28. break;
  29. }
  30. if ("db".equals(s)) {
  31. console.printf("DB content : %s%n",db);
  32. continue;
  33. }
  34.  
  35. // Check DB constraints client-side, then save the user's string.
  36. if (s.length() > MAX_STRING_LENGTH) {
  37. console.printf("String too long, please enter another one.%n");
  38. continue;
  39. }
  40. persistInDatabase(s.toUpperCase());
  41. console.printf("Saved '%s'.%n> ",s);
  42. }
  43. }
  44.  
  45. private void persistInDatabase(String s) {
  46. // Simulate a database constraint
  47. if (s.length() > MAX_STRING_LENGTH) {
  48. throw new IllegalArgumentException("DATA TOO LARGE");
  49. }
  50. db.put(nextId++, s);
  51. }
  52.  
  53. }
Lire la suite...

Become a Guitar Hero with The Coder's Breakfast !

I'm in the process of learning scales, modes and chords for the guitar, but these aren't easy to remember. Being a developer, I thought I could write a small program to help me understand how they are built, and where I can find the related notes on the guitar neck.
So I spent a couple of hours on it tonight (mainly to find documentation on music theory) and here it is : a small companion program that can calculate and display any chord in any mode and key.

The API is very compact and ultra-easy to understand :
(A "Scale" is basically a collection of Notes. It is used to contain notes for whole Scales, Chords, etc.)

  1. Scale scale = Scale.of(Note.C);
  2. System.out.println("Scale of C : " + scale);
  3.  
  4. Scale mode = Mode.IONIAN.of(scale);
  5. System.out.println("Scale of C in "+Mode.IONIAN+" mode : " + mode);
  6.  
  7. Scale chord = Chord.major(mode);
  8. System.out.println("Major chord : " + chord);
  9.  
  10. Guitar.display(chord, Guitar.STANDART_TUNING, 12); // 12 frets

And here is the result :

Scale of C : [C, C#, D, D#, E, F, F#, G, G#, A, A#, B]
Scale of C in IONIAN mode : [C, D, E, F, G, A, B]
Major chord : [C, E, G]
                        3             5             7             9                    12  
--E --||------|------|--G --|------|------|------|------|--C --|------|------|------|--E --|
------||--C --|------|------|------|--E --|------|------|--G --|------|------|------|------|
--G --||------|------|------|------|--C --|------|------|------|--E --|------|------|--G --|
------||------|--E --|------|------|--G --|------|------|------|------|--C --|------|------|
------||------|------|--C --|------|------|------|--E --|------|------|--G --|------|------|
--E --||------|------|--G --|------|------|------|------|--C --|------|------|------|--E --|

The source code is available for download just below.
This ain't rocket science but hey, I thought It might be cool to share it with you :)
Comments and suggestions welcome !