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 2 of 2

Thread: I am the most terrible file reader.....

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

    Default I am the most terrible file reader.....

    I have a program that has 2 options. 1;to read a file and 2, to add a file:





    The text file 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

    the count is declared like this:

    int[] count = {0};


    NOW... If the user reads the program, then adds a file and then reads it again it will look like this:


    Yellow Submarine
    Beatles
    Rock
    Welcome to my life
    Simple plan
    Alternative
    Break
    Three Days Grace
    Rock
    NewSong
    NewArtist
    NewCateg
    Yellow Submarine
    Beatles
    Rock
    Welcome to my life
    Simple plan
    Alternative
    Break
    Three Days Grace
    Rock
    NewSong
    NewArtist
    NewCateg

    There are 24 files.... How can I fix this?

    static void Read (String[] Song, String[] Artist, String[] Category, int[] count, String[] database) throws IOException    //to open and view the database
        {
            BufferedReader reader = new BufferedReader (new FileReader (database [0] + ".txt"));
            System.out.println ("Here is your music collection: ");
            //  System.out.println ("First is a song, followed by the artist and last is the category: ");
            String line = null;
     
     
            while ((line = reader.readLine ()) != null)
            {
                Song [count [0]] = (line);
                line = reader.readLine ();
                Artist [count [0]] = (line);
                line = reader.readLine ();
                Category [count [0]] = (line);
                count [0]++;
            }
     
            reader.close ();
     
            System.out.println ("Song" + "                        " + "Artist" + "                          " + "Category"); // spacing is correct
            System.out.println ();
     
     
            for (int i = 0 ; i < count [0] ; i++)
            {
                System.out.print (Song [i]);
                for (int j = 0 ; j < (30 - Song [i].length ()) ; j++)
                    System.out.print (' ');
     
                System.out.print (Artist [i]);
                for (int j = 0 ; j < (30 - Artist [i].length ()) ; j++)
                    System.out.print (' ');
     
                System.out.print (Category [i]);
                for (int j = 0 ; j < (29 - Category [i].length ()) ; j++)
                    System.out.print (' ');
                System.out.println (' ');
     
            }
            System.out.println ("There are: " + count [0] + " songs");
    Last edited by seaofFire; April 22nd, 2012 at 08:20 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I am the most terrible file reader.....

    Can you post the code for the program?
    Be sure to wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. FILE READER PROGRAM
    By shubhen in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: July 22nd, 2011, 02:31 AM
  2. Java IO File Reader Question???
    By liamShannon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 12th, 2011, 03:37 PM
  3. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM