0

I've been stuck on this assignment for hours and I can't figure this out. I create an array of artists with a size defined by a variable which I increase as more artists get added. If I set the artist[] artistList = new artist[totalArtist];, I get an arrayoutofbounds or just a blank output, so doing artist[] artistList = new artist[1+totalArtist]; works for me so far and at least gives me an output. Any improvements would be nice

Here is a snippet of my code:

public static void main(String[] args) {
        //initialize total artists and id
        int totalArtist = 0;
        int newID = 0;

        //create an array for artists
        artist[] artistList = new artist[1+totalArtist];

        //open the original file
        try {
            File file = new File("p1artists.txt");
            Scanner input = new Scanner(file);

            while (input.hasNextLine()) {
                int id = input.nextInt();
                String name = input.next();

                //create a new artist and put it in the array
                for (int i = 0; i < artistList.length; i++) {
                    artistList[i] = new artist(id, name);
                    totalArtist++;
                }
                //increment to a newID
                newID = id + 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

       for(artist e : artistList)
           System.out.println(e);

My main problem is: Within the for loop, I am able to create a new artist and put it in the artistList array. I am also able to print every element. However, outside the try-catch, it only prints the last element once. I don't understand what I am doing wrong for this to happen.

Please do not suggest array list because if i were able to do it for this assignment, I obviously would.

  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a more specific question. – Joe C Sep 16 '17 at 03:46
  • "able to print every element" -- Where does the code do that? – OneCricketeer Sep 16 '17 at 03:48
  • 1
    Arrays can not change size. To use dynamic sized "arrays", use `List`s instead. – Usagi Miyamoto Sep 16 '17 at 03:48
  • You should really either 1) Use an ArrayList 2) Count the lines of the file before you size the array – OneCricketeer Sep 16 '17 at 03:49
  • @cricket_007 at last 2 lines? – Usagi Miyamoto Sep 16 '17 at 03:49
  • @UsagiMiyamoto No, the question says outside the loop is only one element. That is in the code here. The other print statement is not – OneCricketeer Sep 16 '17 at 03:50
  • @cricket_007, This is an assignment, seriously cannot used Arraylist. I would if I could. I can only print all the elements within the for loop. the last two lines demonstrate how it only prints the last element, once –  Sep 16 '17 at 03:51
  • @dead `new artist[1+totalArtist];`... What is `totalArtist` when that line is evaluated? – OneCricketeer Sep 16 '17 at 03:54
  • 2
    @cricket_007 `artisList` has only 1 element, and never changed size, so the last 2 lines would only print 1 value, that is the last set value... – Usagi Miyamoto Sep 16 '17 at 03:54
  • @UsagiMiyamoto, thanks your last comment cleared that up, but even when I set the array to the amount of lines/artists, it still prints the last element of the array, just 85 times. –  Sep 16 '17 at 03:57
  • @deaddeaddeads Your for loop makes sure, that all previous element is overwritten... Also, you should print out `totalArtist` at the end.... ;-) – Usagi Miyamoto Sep 16 '17 at 04:05
  • Also, "artist" has only one word names? just because if they have more names, you should use `nextLine` instead of `next`... BTW a `nextLine` should be after your current `next` call, too – Usagi Miyamoto Sep 16 '17 at 04:09
  • Post a sample of your "p1artists.txt" file ? – Neeraj Sep 16 '17 at 04:43

3 Answers3

1

Try something like this:

public static void main(String[] args)
{
    // create an array for artists
    artist[] artistList = new artist[0];

    // open the original file
    try {
        final File file = new File("p1artists.txt");
        final Scanner input = new Scanner(file);

        while (input.hasNextLine())
        {
            final int id = input.nextInt();
            final String name = input.next();
            // Skip to next line...
            input.netxLine();

            // Create a ne array, with one more element
            final artist[] newList = new artist[artistList.length + 1];
            // Copy the element references to the new array
            System.arraycopy(artistList, 0, newList, 0, artistList.length);
            // create a new artist and put it in the array
            newList[artistList.length] = new artist(id, name);
            // Replace old array with new one
            artistList = newList;
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

   for(artist e : artistList)
        System.out.println(e);
}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

Your array is only printing one element because you are getting an exception by trying to write to position 2+, which exits the try block, and skips to the foreach loop.

You only have one element in the array

int totalArtist = 0;
artist[] artistList = new artist[1+totalArtist]; // = new artist[1];

Since you cannot use lists, you can either

  1. Read the number of lines of the file, then create the array. Number of lines in a file in Java
  2. Prompt for the number of artists to read from the file
  3. Emulate a List by creating a new array with a larger sized array yourself and copying elements over
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Your artistList.length is always 1. And hence you are always be updating the first and only element. Array can't be extended dynamically. To achieve what you wanted consider ArrayList or LinkedList depending how you foresee the way you consume the items in your resulting list.