I need help with removing arrays or hiding them.
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.
Code java:
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------");
}
}
}
Re: I need help with removing arrays or hiding them.
This thread has been cross posted here:
http://www.javaprogrammingforums.com/whats-wrong-my-code/15293-im-having-problems-removing-arrays.html
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
This thread has been cross posted here:
http://www.javaprogrammingforums.com/whats-wrong-my-code/15294-i-am-most-terrible-file-reader.html#post64368
Although cross posting is allowed,
for everyone's benefit, please read:
Java Programming Forums Cross Posting Rules
The Problems With Cross Posting
You didn't done anything wrong, though I'd highly recommend not opening a fourth thread for this same topic. (The mods really won't like it. They might delete some of them. )
Also, instead of deleting that song, what is it doing?
Are there any Exceptions being thrown or is it simply doing nothing?
Re: I need help with removing arrays or hiding them.
Honestly the for loops are wrong and nothing is happening....
Re: I need help with removing arrays or hiding them.
Why do you have an array that stores 1 number?
Re: I need help with removing arrays or hiding them.
I need to delete only that array and shift the artists and genres that correlate with that song.
I honestly don't know what you mean. It a string array.