Okay so I have an array of Objects.. My goal is to get the output to print every "Song" the user inputs
But for some reason only the last song in the array is printing which is confusing me

import java.util.*;
import java.util.Arrays;
public class SongRunner1
{   
    public static void main(String[] args){
     
     
     String title;
     String t;
     String artist;
     String a;
     int time;
     int tt;
     int rating;
     int rr;
     double totalsum = 0;
     int sum = 0;
     Random r = new Random();
     Scanner console = new Scanner(System.in);
     
     System.out.println("Welcome to your music storing program!");
     System.out.println("How many songs will be inputted?");
     int number = console.nextInt();
     Songs[] IT = new Songs[number];
     
     
         
    for(int i = 1; i <= number; i = i + 1){
        if(i <= number){
      System.out.println("\nThis is song #" + i);  
      String s = console.nextLine();
      System.out.println("What is this songs name?");
       title = console.nextLine();
      System.out.println("Who is the artist of this song?");
       artist = console.nextLine();
        a = artist;
      System.out.println("How long is this song in seconds?");
      time = console.nextInt();
      tt = time;    
          if(time <= 0){
              
                  
          System.out.println("Invalid command!");
          System.out.println("how long is this song in seconds?");
          time = console.nextInt();
          time++;
          
        }

      sum += time;
       rating = r.nextInt(5)+ 1;
       rr = rating;
      totalsum += rating;
        Songs Song1 = new Songs(title, artist, time, rating);
        
       
     if(i >= number){
            System.out.println("=======Song Library Summary=======");
            System.out.println("Title \t \t Artist \t \t Time \t \t");
            for(int b = 0; b < IT.length;b++){
               //System.out.println(Arrays.toString(IT));
               String temp = Song1.toString();
              System.out.println(Song1);
          
           
                
}  

  System.out.println("\n \n" + "The  average rating is:"+ (totalsum)/(double)(number));
            System.out.println("The total time is:"+ sum/60 + ":" + sum%60+ "\n");
}

}

}
}
}

OUTPUT:


Welcome to your music storing program!
How many songs will be inputted?
2

This is song #1
What is this songs name?
Hot N Cold
Who is the artist of this song?
Katy PErry
How long is this song in seconds?
300

This is song #2
What is this songs name?
Ghost
Who is the artist of this song?
Kid Cudi
How long is this song in seconds?
250
=======Song Library Summary=======
Title Artist Time
Ghost Kid Cudi 4:10 1
Ghost Kid Cudi 4:10 1


The average rating is:3.5
The total time is:9:10

----------------------------------------------------------------

So for some reason only the last object of Type Songs is printing. Any help would be tremendously appreciated, I got the code to print the array but only the last one prints I've tried with 4+ diff songs and no matter what only the last one prints...

Have a look at your for-loop. Especially the last if-statement:
if(i >= number)...

See anything that doesnt look right?

Ok, here's a few pointers then. I assume you have a class Songs available to you.

1. The first if-statement in your for-loop is obsolete, you already defined that condition in the for-loop
2. You create the Songs object correct, but you never store it in the Songs[] array
3. You can move the second if-statement out of the for-loop when you are done creating Songs
4. Your error check on negative time is wrong in a few places
5. When you are ready to print the Songs, you make a temp-string but you never use it, either use it or just call print on the object like you are doing.


Just a few pointers, however the main problem lies in my second point. Try to work them out and come back with the rewritten code.

This article has been dead for over six months. Start a new discussion instead.