我不知道该怎么办,因为我确保这些方法没有重复的名称,我试图搜索问题,但似乎每个人的问题与我的相比有所不同,即使输出说了类似的话。
代码:
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?
答案 0 :(得分:1)
问题出在这里:
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。