944,080 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2602
  • Java RSS
Apr 3rd, 2005
0

How can i call my FileOutput method to output the array ?? really confused...

Expand Post »
Hi, I have a fileoutput method which when called, should output the array into a text file. The array is stored in the same public class as the fileoutput method and I am using a constructor for the array, which is in the Cd class. I would like to know how to call this method from within the same class or should i place the file output method in the Cd class. If so, how would i call the method in the main method of public class? i think it should be something like fileoutput(array); ?? i have tried many things but hasn't worked. thanks.

Java Syntax (Toggle Plain Text)
  1.  
  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class MyCdDatabase
  7. {
  8. public static void main (String[] args)
  9. {
  10. int entry = 1;
  11. Cd[] record = new Cd[entry];
  12.  
  13. final int SENTINEL = -1;
  14.  
  15. String album;
  16. String menu;
  17. int MenuChoice;
  18. int i;
  19. int p;
  20.  
  21. int ARRAY_SIZE = 4;
  22.  
  23. Cd[] array = new Cd[ARRAY_SIZE];
  24. {
  25. array[0] = new Cd("Deftones","White Pony","Hard Rock","Maverick",2000,14);
  26. array[1] = new Cd("The Apex Theory","Apossibly","Alternative","Dreamworks",2002,12);
  27. array[2] = new Cd("System Of A Down","Mezmerize","Hard Rock","Sony",2005,14);
  28. array[3] = new Cd("Ill Nino","Confession","Hard Rock","Roadrunner",2003,14);
  29. }
  30.  
  31. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  32. MenuChoice = Integer.parseInt(menu);
  33.  
  34. while (MenuChoice != SENTINEL)
  35. {
  36. if (MenuChoice == 0 )
  37. {
  38. for ( i = 0; i<entry; i++)
  39. {
  40. record[i] = new Cd();
  41. }
  42. for ( i = 0; i<entry; i++)
  43. {
  44. record[i].printcd();
  45. }
  46.  
  47. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  48. MenuChoice = Integer.parseInt(menu);
  49.  
  50. }
  51.  
  52. else if (MenuChoice == 1)
  53. {
  54. for ( p = 0; p<ARRAY_SIZE; p++)
  55. {
  56. array[p].printarray();
  57. }
  58.  
  59. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  60. MenuChoice = Integer.parseInt(menu);
  61.  
  62. }
  63.  
  64. else if (MenuChoice == 2)
  65. {
  66. System.exit(0);
  67. }
  68.  
  69. else if (MenuChoice == 3)
  70. {
  71. album = JOptionPane.showInputDialog("Enter The Album To Search For :");
  72.  
  73. System.out.println("Searching For Album ... : " + album);
  74.  
  75. for ( p = 0; p<ARRAY_SIZE; p++)
  76. {
  77. if (array[p].AlbumName.equals(album))
  78. array[p].printarray();
  79. }
  80.  
  81. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  82. MenuChoice = Integer.parseInt(menu);
  83.  
  84. }
  85.  
  86. else if (MenuChoice == 4)
  87. {
  88.  
  89.  
  90.  
  91. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  92. MenuChoice = Integer.parseInt(menu);
  93.  
  94. }
  95.  
  96. else if (MenuChoice == 5)
  97. {
  98.  
  99.  
  100. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  101. MenuChoice = Integer.parseInt(menu);
  102.  
  103. }
  104.  
  105. else if (MenuChoice == 6)
  106. {
  107.  
  108. menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
  109. MenuChoice = Integer.parseInt(menu);
  110.  
  111. }
  112. }
  113. }
  114. public void fileoutput (Cd[] array) throws IOException
  115. {
  116. final FileWriter outputFile = new FileWriter("CD_Database.txt");
  117. final BufferedWriter OutputBuffer = new BufferedWriter(outputFile);
  118. final PrintWriter printstream = new PrintWriter(OutputBuffer);
  119.  
  120. for(int p = 0; p < array.length; p++)
  121. {
  122. printstream.println(array[p]);
  123. printstream.close();
  124. }
  125.  
  126. final FileReader inputFile = new FileReader("CD_Database.txt");
  127. final BufferedReader inputBuffer = new BufferedReader(inputFile);
  128.  
  129. String line = inputBuffer.readLine();
  130.  
  131. }
  132. }
  133.  
  134. class Cd
  135. {
  136. public String BandName;
  137. public String AlbumName;
  138. public String Genre;
  139. public String RecordLabel;
  140. public int YearReleased;
  141. public int NoOfTracks;
  142.  
  143. public Cd ()
  144. {
  145. BandName = JOptionPane.showInputDialog ("Enter The Artist/s Name : ");
  146. AlbumName = JOptionPane.showInputDialog("Enter The Album Name :");
  147. Genre = JOptionPane.showInputDialog("Enter The Music Genre :");
  148. RecordLabel = JOptionPane.showInputDialog("Enter The Record Label :");
  149. YearReleased = Integer.parseInt(JOptionPane.showInputDialog("Enter The Release Year :"));
  150. NoOfTracks = Integer.parseInt(JOptionPane.showInputDialog("Enter The Number Of Tracks :"));
  151. }
  152.  
  153. public Cd (String b, String a, String g, String r, int y, int n)
  154. {
  155. BandName= b;
  156. AlbumName = a;
  157. Genre = g;
  158. RecordLabel = r;
  159. YearReleased = y;
  160. NoOfTracks = n;
  161. }
  162.  
  163. public void printcd ()
  164. {
  165. String print = "Band: " + BandName + " Album: " + AlbumName + " Genre: " + Genre + " Label: "
  166. + RecordLabel + " Year: " + YearReleased + " Tracks: " + NoOfTracks;
  167.  
  168. System.out.println(print);
  169. }
  170.  
  171. public void printarray ()
  172. {
  173. String array = "Band: " + BandName + " Album: " + AlbumName + " Genre: " + Genre + " Label: "
  174. + RecordLabel + " Year: " + YearReleased + " Tracks: " + NoOfTracks;
  175.  
  176. System.out.println(array);
  177. }
  178.  
  179. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kev269 is offline Offline
2 posts
since Apr 2005
Apr 3rd, 2005
0

Re: How can i call my FileOutput method to output the array ?? really confused...

Hi everyone,

Use the BufferedReader and BufferedWriter classes. Write and read your data appropriately. See the above java class apis for more details

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Number formatting/Help with doubles
Next Thread in Java Forum Timeline: Blocking vs. Non-Blocking Direct Communication





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC