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

Thread: This java program is adding up in a negative way....

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

    Default This java program is adding up in a negative way....

    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 word for word:

    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 23rd, 2012 at 11:17 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: This java program is adding up in a negative way....

    It appears to be adding your entire collection when they add a file.

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

    Default Re: This java program is adding up in a negative way....

    How can I make the program read the original file only once?
    the counter has no limit and if i set one:

    It gives me an error on the 74th line - for (int j = 0 ; j < (30 - Artist [i].length ()) ; j++)

  4. #4
    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: This java program is adding up in a negative way....

    How do you indicate which file is to be read? Do you have the names of the files in a list? How do you go through the list to select its contents one at a time? Where do you move from one filename in the list to the next name in the list?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Adding sound to program.
    By Archibold9 in forum What's Wrong With My Code?
    Replies: 32
    Last Post: December 21st, 2011, 02:28 PM
  2. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  3. Negative
    By xew123 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 07:25 PM
  4. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM