java - 在类型 "rf"中找不到名为 "mp3_organizer"的方法的适用重载

标签 java

我不知道该怎么做,因为我确保方法没有重复的名称,并且我尝试搜索问题,但似乎每个人的问题都与我的不同,即使输出显示类似的内容。

代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;



/**
 *
 * C:\Users\Red\Music\Alan Walker - Hope.mp3
 */

public class mp3_organizer
{

static BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
static final int max = 50;
static final String file = "songlist.txt";

//rf stands for readfile
public static void rf (String[] songname, String[] songpath, String[] artist, String[] genre, int[] count, String file) throws FileNotFoundException, IOException
{
    BufferedReader reader = new BufferedReader (new FileReader (file));

    String temp, line = null;

    while ((line = reader.readLine ()) != null)
    {
        songname [count [0]] = line;
        songpath [count [0]] = reader.readLine ();
        artist [count [0]] = reader.readLine ();
        genre [count [0]] = reader.readLine ();
        count [0]++;
    }


    reader.close ();
}


public static void newsong (String file) throws IOException
{
    BufferedWriter writer = new BufferedWriter (new FileWriter (file, true));
    System.out.println ("[ADDING songS] \n");
    System.out.println ("Enter the name of the song");
    String name = stdin.readLine ();
    System.out.println ("Enter a file address of song");
    String filePath = stdin.readLine ();
    System.out.println ("Enter the artist's name or put N/A if unknown");
    String songArtist = stdin.readLine ();
    System.out.println ("Enter the genre");
    String songGenre = stdin.readLine ();

    writer.write ("");
    writer.newLine ();
    writer.write (name);
    writer.newLine ();
    writer.write (filePath);
    writer.newLine ();
    writer.write (songArtist);
    writer.newLine ();
    writer.write (songGenre);
    writer.newLine ();
    writer.close ();
}


static void sort (String[] songname, String[] artist, String[] genre, int count) throws IOException
{
    BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    boolean swapped = false;
    String temp;
    int select;

    System.out.println ("What would you like to sort the database by?\n(1) song Title\n(2) Artist \n(3) Genre");
    do
    {
        select = Integer.parseInt (stdin.readLine ());
        switch (select)
        {
            case 1:
                for (int i = 0 ; i < (count - 1) ; i++)
                {
                    swapped = false;
                    for (int j = 0 ; j < ((count - 1) - i) ; j++)
                    {
                        if (songname [j].compareTo (songname [j + 1]) > 0)
                        {
                            temp = songname [j];
                            songname [j] = songname [j + 1];
                            songname [j + 1] = temp;

                            temp = artist [j];
                            artist [j] = artist [j + 1];
                            artist [j + 1] = temp;

                            temp = genre [j];
                            genre [j] = genre [j + 1];
                            genre [j + 1] = temp;
                            swapped = true;
                        }
                    }
                    if (!swapped)
                    {
                        i = count;
                    }
                }
                break;
            case 2:
                for (int i = 0 ; i < (count - 1) ; i++)
                {
                    swapped = false;
                    for (int j = 0 ; j < ((count - 1) - i) ; j++)
                    {
                        if (artist [j].compareTo (artist [j + 1]) > 0)
                        {
                            temp = songname [j];
                            songname [j] = songname [j + 1];
                            songname [j + 1] = temp;

                            temp = artist [j];
                            artist [j] = artist [j + 1];
                            artist [j + 1] = temp;

                            temp = genre [j];
                            genre [j] = genre [j + 1];
                            genre [j + 1] = temp;
                            swapped = true;
                        }
                    }
                    if (!swapped)
                    {
                        i = count;
                    }
                }
                break;
            case 3:
                for (int i = 0 ; i < (count - 1) ; i++)
                {
                    swapped = false;
                    for (int j = 0 ; j < ((count - 1) - i) ; j++)
                    {
                        if (genre [j].compareTo (genre [j + 1]) > 0)
                        {
                            temp = songname [j];
                            songname [j] = songname [j + 1];
                            songname [j + 1] = temp;

                            temp = artist [j];
                            artist [j] = artist [j + 1];
                            artist [j + 1] = temp;

                            temp = genre [j];
                            genre [j] = genre [j + 1];
                            genre [j + 1] = temp;
                            swapped = true;
                        }
                    }
                    if (!swapped)
                    {
                        i = count;
                    }
                }
                break;
            default:
                System.out.print ("\nPlease enter a valid option: ");
                break;
        }
    }
    while (select != 1 && select != 2 && select != 3);
}



static void search (String[] songname, String[] songpath, String[] artist, String[] genre, int count) throws IOException
{
    BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
    String search;
    boolean find = false;
    int select;

    System.out.println ("Please select what category you wish to search by:\n(1) song\n(2) Artist\n(3) Genre\n(4) Song Path\n(0) Cancel");
    do
    {
        select = Integer.parseInt (stdin.readLine ());

        switch (select)
        {
            case 1:
                System.out.print ("Please enter the name of the song to search for: ");
                search = stdin.readLine ();
                for (int i = 0 ; i < count ; i++)
                {
                    if (songname [i].equalsIgnoreCase (search))
                    {
                        find = true;
                    }
                }
                if (find)
                {
                    System.out.println ("song\tsong\tArtist\tGenre");
                    for (int i = 0 ; i < count ; i++)
                    {
                        if (songname [i].equalsIgnoreCase (search))
                        {
                            System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
                        }
                    }
                }
                else
                {
                    System.out.println (search + " not found.");
                }
                break;

            case 2:
                System.out.print ("Please enter the name of the artist to search for: ");
                search = stdin.readLine ();
                for (int i = 0 ; i < count ; i++)
                {
                    if (artist [i].equalsIgnoreCase (search))
                    {
                        find = true;
                    }
                }
                if (find)
                {
                    System.out.println ("song\tsongpath\tArtist\tGenre");
                    for (int i = 0 ; i < count ; i++)
                    {
                        if (artist [i].equalsIgnoreCase (search))
                        {
                            System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
                        }
                    }
                }
                else
                {
                    System.out.println ("Artist not found");
                }
                break;

            case 3:
                System.out.print ("Please enter the genre to search for: ");
                search = stdin.readLine ();
                for (int i = 0 ; i < count ; i++)
                {
                    if (genre [i].equalsIgnoreCase (search))
                    {
                        find = true;
                    }
                }
                if (find)
                {
                    System.out.println ("song\tsongpath\tArtist\tGenre");
                    for (int i = 0 ; i < count ; i++)
                    {
                        if (genre [i].equalsIgnoreCase (search))
                        {
                            System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
                        }
                    }
                }
                else
                {
                    System.out.println (search + " cannot be found.");
                }
                break;

            case 4:
                System.out.print ("Please enter the file address of the song to search for: ");
                search = stdin.readLine ();
                for (int i = 0 ; i < count ; i++)
                {
                    if (songname [i].equalsIgnoreCase (search))
                    {
                        find = true;
                    }
                }
                if (find)
                {
                    System.out.println ("song\tsongpath\tArtist\tGenre");
                    for (int i = 0 ; i < count ; i++)
                    {
                        if (songname [i].equalsIgnoreCase (search))
                        {
                            System.out.println (songname [i] + "\t" + songpath [i] + "\n" + artist [i] + "\t" + genre [i]);
                        }
                    }
                }
                else
                {
                    System.out.println ("File address not found.");
                }
                break;

            case 0:
                break;

            default:
                System.out.println ("Please enter a valid option: ");
        }
    }


    while (select > 3);
    System.out.println ();
}


public static void backup (String file, int count, String[] songname, String[] songpath, String[] artist, String[] genre) throws IOException
{
    BufferedWriter writer = new BufferedWriter (new FileWriter ("backup.txt"));
    for (int i = 0 ; i < count ; i++)
    {
        writer.write (songname [i]);
        writer.newLine ();
        writer.write (songpath [i]);
        writer.newLine ();
        writer.write (artist [i]);
        writer.newLine ();
        writer.write (genre [i]);
        writer.newLine ();
    }


    writer.close ();
}


public static void printlist (int count, String[] songname, String[] songpath, String[] artist, String[] genre)
{
    for (int i = 0 ; i < count ; i++)
    {
        System.out.println (songname [i] + "\n" + songpath [i] + "\n" + artist [i] + "\n" + genre [i]);
    }
}


public static void test ()
{
    File songlist = new File ("songlist.txt");

    System.out.println (System.getProperty ("user.dir"));

    if (songlist.exists ())
    {
        if (songlist.canRead ())
        {
            System.out.print ("file can be read");
        }

        else
        {
            System.out.print ("File cannot be read");
        }
    }


    else
    {
        System.out.println ("file does not exist");
    }
}


public static void main (String str[]) throws FileNotFoundException, IOException
{
    boolean reset = true;

    test ();

    do
    {

        int count = 0;
        String[] songname = new String [max];
        String[] songpath = new String [max];
        String[] artist = new String [max];
        String[] genre = new String [max];

        rf (songname, songpath, artist, genre, count, file);

        BufferedReader reader = new BufferedReader (new FileReader ("songlist.txt"));
        System.out.println ("[OPTIONS] \n" +
                "1) Print the song list \n" +
                "2) Add a song to list \n" +
                "3) Search for a song \n" +
                "4) Sort the song list \n" +
                "5) Create a backup of the song lsit \n" +
                "6) Delete a song from the collection \n" +
                "7) Create a new profile");

        System.out.println ("Enter a option");
        int choice = Integer.parseInt (stdin.readLine ());

        switch (choice)
        {
            case 1:
                System.out.println ("[PRINTING song LIST] \n");
                printlist (count, songname, songpath, artist, genre);
                break;

            case 2:
                System.out.println ("[ADDING SONG]");
                newsong (file);
                break;

            case 3:
                System.out.println ("[SONG SEARCH]");
                search (songname, songpath, artist, genre, count, file);
                break;

            case 4:
                System.out.println ("[SONGLIST SORT]");
                sort (songname, artist, genre, count);
                break;

            case 5:
                System.out.println ("[CREATING BACKUP OF SONGLIST");
                backup (file, count, songname, songpath, artist, genre);
                break;

            case 6:
                System.out.println ("[REMOVE SONG FROM LIST]");
                break;

            case 7:
                System.out.println ("[CREATE NEW PROFILE]");
                break;
        }
    }
    while (reset == true);
}
}

输出:

No applicable overload for the method named "rf" was found in type
"mp3_organizer". Perhaps you wanted the overloaded version "void rf(java.lang.String[] songname, java.lang.String[] 
songpath,java.lang.String[] artist, java.lang.String[] genre, int[] count,java.lang.String file) throws java.io.FileNotFoundException,
java.io.IOException;" instead?

最佳答案

问题出在这里:

public static void rf (String[] songname, String[] songpath, String[] artist, String[] genre, int[] count, String file){
   //...
}

您的方法实现中有六个参数,并且 int cuunt[] 是一个数组

但是当您调用该方法时,传递六个参数,但算作变量:

rf (songname, songpath, artist, genre, count, file);

要解决此问题您需要重载该方法以将 count 作为变量传递以与方法实现相匹配。如果您只想作为变量传递,否则更改为数组。像这样:

//method count array
public static void rf (String[] songname, String[] songpath, String[] artist, String[] genre, int[] count, String file) throws FileNotFoundException, IOException
{
    BufferedReader reader = new BufferedReader (new FileReader (file));

    String temp, line = null;

    while ((line = reader.readLine ()) != null)
    {
        songname [count [0]] = line;
        songpath [count [0]] = reader.readLine ();
        artist [count [0]] = reader.readLine ();
        genre [count [0]] = reader.readLine ();
        count [0]++;
    }


    reader.close ();
}

//overloaded six parameter method
//without count array. count variable
public static void rf (String[] songname, String[] songpath, String[] genre, int count, String file) throws FileNotFoundException, IOException
    {
        BufferedReader reader = new BufferedReader (new FileReader (file));

        String temp, line = null;

        while ((line = reader.readLine ()) != null)
        {
            songname [count [0]] = line;
            songpath [count [0]] = reader.readLine ();
            artist [count [0]] = reader.readLine (); - remove this line
            genre [count] = reader.readLine ();//change this too, genre [count[0]]
            count++;//since this is not a array change to count[0]++.
        }


        reader.close ();
    }

您还需要重载 search()

static void search (String[] songname, String[] songpath, String[] artist, String[] genre, int count)

与调用函数不匹配(file参数不在实现中):

search (songname, songpath, artist, genre, count, file);

更新:

这些也必须根据您的上述实现进行更改。

songname [count [0]] = line;
songpath [count [0]] = reader.readLine ();
artist [count [0]] = reader.readLine ();
genre [count [0]] = reader.readLine ();
count [0]++;

我认为,您可以将 count 参数作为数组传递给 rf()search(),而不是更改所有这些。否则你需要改变整个程序。这取决于你想办法做到这一点,

了解更多关于method overloading的信息.

关于java - 在类型 "rf"中找不到名为 "mp3_organizer"的方法的适用重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234627/

相关文章:

java - AES CMAC 计算 - 主机密码的输出长度不正确

java - 如何根据输入长度更改 DecimalFormat 行为?

java - 如何使用 Weka API 在 J48/C4.5 上进行 10 次交叉验证后保存最佳树

java - ListView 未反射(reflect) notifyDataSetChanged() 的更改

java - MigLayout 换行两行

java - Android游戏-当实体从列表中删除时的屏幕闪烁

java - 如何在 BigQuery 中创建非分区表。并为 BigQuery 中的表导出表 SQL?

java - 修改tomcat默认根目录

java - Play! 中的 "reverse controller"是什么?框架?

java - MediaCodec h264 编码器输出大量原始流