Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 20th, 2012  |  syntax: None  |  size: 1.63 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Storing and accessing strings in Java (by using arrays)
  2. import javax.swing.*; // import the swing library for I/O
  3.  
  4. class favsongs
  5. {
  6.  
  7.    public static void main (String[] param)
  8. {
  9.       music();
  10.       System.exit(0);
  11.  
  12. } // END main
  13.  
  14.  
  15. /* ***************************************************
  16.    Set up an array containing songs then find one asked for by the user
  17. */
  18.  
  19. public static void music()
  20. {
  21.  
  22.    // Declare variables
  23.    //
  24. String key =""; //the thing looked for
  25. int result = -1;// the position where the answer is found
  26. String[] songs = new String[5];
  27.  
  28.  
  29.  
  30.  
  31.  
  32. // Ask for songs
  33. for (p=0; p<=4; p++)
  34. {  
  35.  
  36.          songs[p]=JOptionPane.showInputDialog("Song "+ p + "?");
  37.     }
  38.  
  39. // Ask user for a song
  40. key = JOptionPane.showInputDialog("Name a song and i'll tell you what position you placed it in.");
  41.  
  42.     for (int i=0; i<songs.length; i++)
  43.     {
  44.  
  45.        if (songs[i].equals(key))
  46.        {
  47.           result = i;  
  48.        }
  49.        else // Error message
  50.        {
  51.        JOptionPane.showMessageDialog(null,"Error!!");
  52.        break;
  53.        }
  54.     }
  55.     // Tells user the name of the song and what position in the array it is in
  56.    JOptionPane.showMessageDialog(null,"You placed " + key + " in position " + " " + result);
  57.  
  58. } // END music
  59.  
  60.  } // END class favsongs
  61.        
  62. for (p=1; p<=4; p++)
  63.        
  64. for (int i=0; i<songs.length; i++)
  65. {
  66.    if (songs[p].equals(key))
  67.        
  68. // Ask for songs
  69. for (p=1; p<=4; p++)
  70. {      
  71.  
  72.      songs[p]=JOptionPane.showInputDialog("Song "+ p + "?");
  73. }
  74.        
  75. for (p=1; p<=4; p++)
  76. {  
  77.     songs[p]=JOptionPane.showInputDialog("Song "+ p + "?");
  78. }
  79.        
  80. for (p=0; p < song.length(); p++)
  81. {  
  82.     songs[p]=JOptionPane.showInputDialog("Song "+ p + "?");
  83. }