| | |
How can i call my FileOutput method to output the array ?? really confused...
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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)
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); } }
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 8
Re: How can i call my FileOutput method to output the array ?? really confused...
0
#2 Apr 3rd, 2005
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
Use the BufferedReader and BufferedWriter classes. Write and read your data appropriately. See the above java class apis for more details
Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- Array Sorting on a User Supplied Element (Java)
- How to call a EJB method from Session bean method (Java)
- Generic method parser / invoker (C#)
- funny output while doing multiplication of two 2d array (C++)
- Array Values not found in Methods (Java)
- help with polymorphism and inheritance...getting null values (Java)
Other Threads in the Java Forum
- Previous Thread: turning an image to binary code
- Next Thread: Blocking vs. Non-Blocking Direct Communication
Views: 2363 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows





