Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I'm having problems with removing arrays

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I'm having problems with removing arrays

    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------");
    }
    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm having problems with removing arrays

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. Unformatted code is hard to read and understand.

    Try debugging your code by adding println statements to display the values of all the variables used to control the logic and access the array's contents in the loops. The output will help you understand what the code is doing.
    Last edited by Norm; April 22nd, 2012 at 07:40 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm having problems with removing arrays

    Please remove this or tell me how I can remove this part of the forum.

    Wow, I am terribly sorry. Usually I can delete my posted topics in forums but this????????
    Last edited by seaofFire; April 23rd, 2012 at 11:21 AM.

Similar Threads

  1. Help removing JFrame inheritance
    By SendBorg in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2012, 08:10 PM
  2. Java Not Removing Panels?
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 07:54 PM
  3. Help: Bug with removing data from a Hash Map
    By seabeach in forum Collections and Generics
    Replies: 4
    Last Post: August 11th, 2011, 11:54 AM
  4. Arraylist removing element
    By Stn in forum Loops & Control Statements
    Replies: 6
    Last Post: January 9th, 2011, 08:14 PM
  5. Arrays: question about "removing" items.
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2009, 04:09 AM