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.
import javax.swing.*;
import java.util.*;
import java.io.*;
public class MyCdDatabase
{
public static void main (String[] args)
{
int entry = 1;
Cd[] record = new Cd[entry];
final int SENTINEL = -1;
String album;
String menu;
int MenuChoice;
int i;
int p;
int ARRAY_SIZE = 4;
Cd[] array = new Cd[ARRAY_SIZE];
{
array[0] = new Cd("Deftones","White Pony","Hard Rock","Maverick",2000,14);
array[1] = new Cd("The Apex Theory","Apossibly","Alternative","Dreamworks",2002,12);
array[2] = new Cd("System Of A Down","Mezmerize","Hard Rock","Sony",2005,14);
array[3] = new Cd("Ill Nino","Confession","Hard Rock","Roadrunner",2003,14);
}
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
while (MenuChoice != SENTINEL)
{
if (MenuChoice == 0 )
{
for ( i = 0; i<entry; i++)
{
record[i] = new Cd();
}
for ( i = 0; i<entry; i++)
{
record[i].printcd();
}
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
else if (MenuChoice == 1)
{
for ( p = 0; p<ARRAY_SIZE; p++)
{
array[p].printarray();
}
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
else if (MenuChoice == 2)
{
System.exit(0);
}
else if (MenuChoice == 3)
{
album = JOptionPane.showInputDialog("Enter The Album To Search For :");
System.out.println("Searching For Album ... : " + album);
for ( p = 0; p<ARRAY_SIZE; p++)
{
if (array[p].AlbumName.equals(album))
array[p].printarray();
}
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
else if (MenuChoice == 4)
{
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
else if (MenuChoice == 5)
{
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
else if (MenuChoice == 6)
{
menu = JOptionPane.showInputDialog("Enter New Entry = 0, Print Database = 1, Quit = 2, Search = 3, Output Database = 4, Input Database From File = 5, Sort = 6");
MenuChoice = Integer.parseInt(menu);
}
}
}
public void fileoutput (Cd[] array) throws IOException
{
final FileWriter outputFile = new FileWriter("CD_Database.txt");
final BufferedWriter OutputBuffer = new BufferedWriter(outputFile);
final PrintWriter printstream = new PrintWriter(OutputBuffer);
for(int p = 0; p < array.length; p++)
{
printstream.println(array[p]);
printstream.close();
}
final FileReader inputFile = new FileReader("CD_Database.txt");
final BufferedReader inputBuffer = new BufferedReader(inputFile);
String line = inputBuffer.readLine();
}
}
class Cd
{
public String BandName;
public String AlbumName;
public String Genre;
public String RecordLabel;
public int YearReleased;
public int NoOfTracks;
public Cd ()
{
BandName = JOptionPane.showInputDialog ("Enter The Artist/s Name : ");
AlbumName = JOptionPane.showInputDialog("Enter The Album Name :");
Genre = JOptionPane.showInputDialog("Enter The Music Genre :");
RecordLabel = JOptionPane.showInputDialog("Enter The Record Label :");
YearReleased = Integer.parseInt(JOptionPane.showInputDialog("Enter The Release Year :"));
NoOfTracks = Integer.parseInt(JOptionPane.showInputDialog("Enter The Number Of Tracks :"));
}
public Cd (String b, String a, String g, String r, int y, int n)
{
BandName= b;
AlbumName = a;
Genre = g;
RecordLabel = r;
YearReleased = y;
NoOfTracks = n;
}
public void printcd ()
{
String print = "Band: " + BandName + " Album: " + AlbumName + " Genre: " + Genre + " Label: "
+ RecordLabel + " Year: " + YearReleased + " Tracks: " + NoOfTracks;
System.out.println(print);
}
public void printarray ()
{
String array = "Band: " + BandName + " Album: " + AlbumName + " Genre: " + Genre + " Label: "
+ RecordLabel + " Year: " + YearReleased + " Tracks: " + NoOfTracks;
System.out.println(array);
}
}