Writing data to a txt file file using java classes -


i trying understand why code not writing output textfile expect work. program takes filename command line argument, , prints text file screen. bit more complicated since uses classes , objects demonstrate how objects work. can decipher why not writing file? here's code:-

public class mamoonp3test {      public static void main(string[] args) throws exception {          //create array of 10 guitar (mamoonp3) objects         final int number_of_instances = 10;         mamoonp3[] objectnames = new mamoonp3[number_of_instances];          try          {                    string filename = new string(args[0]);              for(int i=0; i<number_of_instances; i++) {                 objectnames[i] = new mamoonp3(filename);                 system.out.println("this guitar number: " + i);                 objectnames[i].tuneguitar();                 objectnames[i].playguitar();                 objectnames[i].displayacronym();                 objectnames[i].stopguitar();                 system.out.println("---------------------------");                 }         }         catch (exception e)         {             system.out.println("please provide input file");             system.out.println("usage: java mamoonp3test filename.txt");         }     }  } 

import java.io.*;  public class mamoonp3 {      final int number_of_strings = 6;     char[] stringnames = {'e','a','d','g','b','e'};     int[] stringnumbers = {6,5,4,3,2,1};     string[] stringpitch = {"sixth","fifth","fourth","third","second","first"};      boolean istuned;     boolean isplaying;     string stringacronym = new string("even after dinner giant boys eat");      //create printwriter output     printwriter output;      public mamoonp3(string filename) throws exception{         istuned = false;         isplaying = false;         // create target file         file targetfile = new file(filename);         //create printwriter output         output = new printwriter(targetfile);     }      public void tuneguitar() {         system.out.println("the guitar tuned.");         (int i=0; i<number_of_strings; i++) {             system.out.println(stringnames[i] + " string number " + stringnumbers[i] + " , ranked " + stringpitch[i] + " in pitch");             output.print(stringnames[i] + " string number " + stringnumbers[i] + " , ranked " + stringpitch[i] + " in pitch");             output.close();         }     }      public void playguitar() {         system.out.println("the guitar playing.");         output.print("the guitar playing.");         output.close();     }      public void stopguitar() {         system.out.println("the guitar stoped.");         output.print("the guitar stoped.");         output.close();     }      public void displayacronym() {         system.out.println("always remember string names!");         system.out.println("heres reminder: " + stringacronym);         output.print("always remember string names!");         output.print("heres reminder: " + stringacronym);         output.close();     }  } 

you're setting file of object nothing with, you're not writing with,

mamoonp3 newobject = new mamoonp3(filename); 

... , not setting file in objects try write with. check constructors using: every manoop3 object created in loop. see so, check constructors you're using

i suggest change approach entirely.

  • get file input , output out of mamoonp3 class.
  • instead, class should concern representing state of musical instrument, , nothing else.
  • give class decent tostring() override method.
  • i & o should go elsewhere in separate class of own.
  • give i&o class method allows pass mamoonp3 objects can written.
  • as aside, never use new string(anything). use args[0].
  • always close printwriter when done writing. causing error.

edit

possibly way solve this:

  • create printwriter object in main method.
  • give manoop3 class printwriter field , constructor takes printwriter , sets field it.
  • write printwriter in manoop3, don't close it.
  • then close printwriter in main method when manoop3 objects have completed use of it.

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -