Ok I have a text file that looks like this:
Yellow Submarine
Beatles
Rock
Welcome to my life
Simple plan
Alternative
Break
Three Days Grace
Rock
There are 9 files and 3 arrays. One for songs, one for artists and one for genre.
I want to delete or remove from the array the song "Welcome to my life"
I want to delete it by pushing it out to the end of the array, then overwriting it so it doesn't show.
My for loops are wrong. Can you help me with my code? Il really appreciate it.
static void Delete (String[] Song, String[] Artist, String[] Category, int[] count, String[] database) throws IOException //Delete a song from the collection
{
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
BufferedReader reader = new BufferedReader (new FileReader (database [0] + ".txt"));
BufferedWriter writer = new BufferedWriter (new FileWriter (database [0] + ".txt", true));
String deletechoice; //asks user what to delete
String temp; // temp files
// reads file
String line1 = null;
while ((line1 = reader.readLine ()) != null)
{
Song [count [0]] = (line1);
line1 = reader.readLine ();
Artist [count [0]] = (line1);
line1 = reader.readLine ();
Category [count [0]] = (line1);
}
reader.close ();
// asks user to locate song
System.out.println ();
System.out.println ("What song would you like to delete?: ");
deletechoice = (stdin.readLine ());
for (int k = 0 ; k < count [0] ; k++) //searches efficently for the file
{
boolean Found = false;
if (deletechoice.equalsIgnoreCase (Song [k]))
{
System.out.println ("It exists, so it will be deleted " + k);
Found = true;
for (int i = count [0] ; i < count [0] - k ; i++)
{
temp = Song [i];
Song [i] = Song [i - 1];
Song [i - 1] = temp;
temp = Artist [i];
Artist [i] = Artist [i - 1];
Artist [i - 1] = temp;
temp = Category [i];
Category [i] = Category [i - 1];
Category [i - 1] = temp;
}
//overwrites it
for (int j = 0 ; j < count [0] - k ; j++)
{
writer.write (Song [j]);
writer.newLine ();
writer.write (Artist [j]);
writer.newLine ();
writer.write (Category [j]);
writer.newLine ();
}
writer.close ();
}
else if (!(deletechoice.equals (Song [k])))
{
System.out.print ("----searching------");
}
}
}