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?
Code java:
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");
Re: This java program is adding up in a negative way....
It appears to be adding your entire collection when they add a file.
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++)
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?