Mikecat119
Mikecat119

Reputation: 1

An error message is telling me a method is undefined but it appears to be defined

This is the driver class which includes the method which receives the error message, "The method ReadSongArray(File, int) is undefined for the type SongArray." I'm not sure what's going wrong here because I made sure to create an object of type SongArray in my driver class.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ArrayDriver {

    public void main(String[] args){
        File file1 = new File("TenKsongs.csv");
        SongArray drive = new SongArray();
        drive.ReadSongArray(file1, 10);
    }
}

Here is the SongArray class.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class SongArray {

public Song[] ReadSongArray(File file, int numsongs){
    File file1=new File("TenKsongs.csv");
    Song[] songDB;
    songDB=new Song[numsongs];
    int i=0;
    try{
        FileReader file_reader=new FileReader(file1);
        BufferedReader buf_reader = new BufferedReader (file_reader);
        while(i<numsongs){
            String line=buf_reader.readLine();
            String[] data=line.split(",");// in csv file, attributes are separate using ","
            //transfer string to float and int
            float duration_StrF=Float.parseFloat(data[3]);
            int digitalid_StrInt=Integer.parseInt(data[4]);

            String title_rmSP=data[1].replaceAll("\\s+", "");//remove spaces in song title and artist name
            String artist_rmSP=data[2].replaceAll("\\s+", "");


            Song chips = new Song(title_rmSP,artist_rmSP,duration_StrF,digitalid_StrInt);

            i++;
        }
        buf_reader.close();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    return (songDB);
}

}

Upvotes: 0

Views: 4252

Answers (3)

AshanPerera
AshanPerera

Reputation: 628

What you can try to do is make sure all your classes are in the same package to avoid namespace confusions.

Create a new package
eg. com.test.mike

Copy all your files into this package. So that they will referenced as follows on your class path.
com.test.mike.SongArray
com.test.mike.ArrayDriver
com.test.mike.Song

Upvotes: 0

JNYRanger
JNYRanger

Reputation: 7097

The issue you are experiencing is because your SongArray class is missing a few components if you are going to make it an object instance. You should read up on Object Oriented Programming, but by calling new SongArray() is telling Java to create an instance of SongArray You need to add a constructor to your class to do this, unless you are making a static class that you do not create an instance of, but instead pass parameters to it with never calling new.

public class SongArray
{
    //public or private components
    private Song[] songDB;
    private int i;

     //default constructor 
    public SongArray()
     {
        //build components of the SongArray object such as:
        songDB = new Song[100];
     }
     //overloaded constructor
     public SongArray(int count, Song[] songs)
     {
          songDB = songs;
          i = count;
    }
    //other components of class and various functions such as:
    public Song[] readSongArray(File f, int numsong)
    {
        //do stuff here like in your question
    }

}

You should create static classes if you do not want to or cannot instantiate them.

You can learn more about OOP from Java/Oracle directly here: http://docs.oracle.com/javase/tutorial/java/concepts/

Upvotes: 0

Rob
Rob

Reputation: 526

You may be using an old version of the class that doesn't have that method in the classpath. Try saving the source code file, recompile, redeploy, and restart the server.

It's things like this that will drive a developer mad.

Upvotes: 1

Related Questions