Question

I have two problems that I am dealing with in my create a class of string instruments problem. First the requirements are:

  1. The name of the output file in the code of main must be the name specified on the command line, where you find the required code. As specified in the requirements, the test class must be started with an argument in the command line:

java Mynamep3tst myfilename.txt here myfilename.txt is the file where all output must go. This file name should be used in the program as follows:

java.io.File file = new java.io.File(args[0]);

java.io.PrintWriter output = new java.io.PrintWriter(file);

and when you have a message to be sent to the file,

output.println(message);

I keep receiving an error at this point:

public static void printInstrumentArray(StringInstrument[] instruments)throws Exception{

        java.io.File file = new java.io.File(args[0]);

        java.io.PrintWriter output = new java.io.PrintWriter(file);

the error says "cannot find symbol" which I know is what it does when I haven't declared the array. When I do declare the array args[] it says "Exception in thread "main" java.lang.NullPointerException".

Next problem

  1. When I run this it works but it keeps popping up the error "exporting non-public type through public API". I have tried putting public at "StringInstrument[] instruments;". However it will pop up the error "illegal start of operation".

      public static void main(String[] args) throws Exception{//begin main
     //declare instruments
     StringInstrument[] instruments;
    
     //create instrument array
     instruments = createInstrumentArray();
    
     //Print instrument array
     printInstrumentArray(instruments);
      }//end main 
    
      //Create an array of instrument objects
      public static StringInstrument[] createInstrumentArray(){//begin method
        StringInstrument[] instruments = new StringInstrument [10];
    
        //Loop that inputs random integers into the array
            for (int i = 0; i < instruments.length; i++){//begin loop
                instruments[i] = new StringInstrument();
    
            }//end loop
          return instruments;
          }//End method           
         public static void printInstrumentArray(StringInstrument[] instruments)throws Exception{
    
        java.io.File file = new java.io.File(args[0]);
    
        java.io.PrintWriter output = new java.io.PrintWriter(file);
        //declare and initialize arrays
        String[] instrumentList = new String [10];
        int[] stringNumber = new int [10];
        //input string names into array
        instrumentList[0] = "Guitar";
        instrumentList[1] = "Violin";
        instrumentList[2] = "Bass Guitar";
        instrumentList[3] = "Cello";
        instrumentList[4] = "Banjo";
        instrumentList[5] = "Sitar";
        instrumentList[6] = "Rabab";
        instrumentList[7] = "Viola";
        instrumentList[8] = "Harp";
        instrumentList[9] = "Ukulele";
    
        stringNumber[0] = 5;
        stringNumber[1] = 4;
        stringNumber[2] = 5;
        stringNumber[3] = 4;
        stringNumber[4] = 5;
        stringNumber[5] = 18;
        stringNumber[6] = 3;
        stringNumber[7] = 4;
        stringNumber[8] = 47;
        stringNumber[9] = 4;
            //Print an array of instruments and their actions
            for (int i = 0; i < instruments.length; i++){//begin for loop
                instruments[i].setInstrumentName(instrumentList[i]); 
                output.println(instruments[i].instrumentNameDisplay());
                output.println(instruments[i].numberOfStrings(stringNumber[i]));
                output.println(instruments[i].tuneInstrument());
                output.println(instruments[i].playInstrument());    
                output.println(instruments[i].playInstrumentBand());
                output.println(" ");
    
            }//end for loop
            output.close();
        }//end method      
        }//end class
    
Was it helpful?

Solution

For the first problem, you need to pass a args from main to printInstrumentArray as a parameter. args is only visible to main.

As for the second problem, the problem is that you have a function declared as public (meaning that external classes can use it) returning a reference to an instance of a class declared as private (meaning that external classes can't use it). This is a contradiction. Either make StringInstrument public or make createInstrumentArray private.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top