import java.util.*;
import java.util.Arrays;
public class SongRunner1
{   
    public static void main(String[] args){
  
     String title;
    
     String artist;
     
     int time;
     int rating;
     String String1;

        
    
    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];
  
   

   double totalsum = 0;
   int sum = 0;
   Songs Song1;
   
   
   
   // String Song1 = Songs[s1].toString();
    for(int i = 1; i <= number; i = i + 1){
        if(i <= number){
        

      System.out.println("\nThis is song #" + i);
      System.out.println("What is this songs name?");
       String s = console.next();
       String t;
       t = console.nextLine();
       title = t;

      System.out.println("Who is the artist of this song?");
       artist = console.nextLine();
      System.out.println("How long is this song in seconds?");
       time = console.nextInt();
      
          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;
      totalsum += rating;

      
     
       Song1 = new Songs(t, artist, time, rating);
       

     if(i >= number){
       
            System.out.println("=======Song Library Summary=======");
            System.out.println("Title \t \t Artist \t \t Time \t \t");
            System.out.println(Song1.toString());
           // String Song1 = IT.toString(title,artist,time,ratin);
           // System.out.println(IT[SongRunner1].toString());
        
            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?
1

This is song #1
What is this songs name?
Truth
Who is the artist of this song?
Justice
How long is this song in seconds?
200

=======Song Library Summary=======
Title Artist Time
null null 0:0 3


The average rating is:3.0
The total time is:3:20

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

I want the array of song objects to be able to be printed in the output
here is my build for the Songs class.

package serious;
import java.util.Scanner;
import java.util.*;
import java.util.Arrays;


/**
 * This is a virtual music library.
 * 
 * @version (1.0)
 */
public class Songs
{

     
    private String title;
    private String artist;
    private int time;
    private int rating;
    private String String1;
    
   
    
    Random r = new Random();
 
    public String getTitle(){
        return title;
    }
    public void setTitle(String newTitle){
        title = newTitle;
    }
    public String getArtist(){
        return artist;
    }
    public void setArtist(String newArtist){
        artist = newArtist;
         
       
    
    }
    public int getTime(){
      return time;  
        
    }
    public void setTime(int newTime){
        time = newTime;
        
    }
    public int getRating(){
         return rating;
    }
    
   public void setRating(){
       rating = r.nextInt(5-1);
    }
    public String toString(){
         String1 = (title + "\t" + artist+ "\t" + time/60 + ":" + time%60 + "\t" +
        rating);
        return String1;
    }
    public Songs(String t, String a , int tt , int rr )
    {
     
        setTitle(title);
        setArtist(artist);
        setTime(time);
        setRating();
        toString();
        
    }
    
    
    
    }

I am curious on how to actually print each song's contents in the SOng Library Summary, I am having issues. Thanks

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Look at your Songs constructor. The error is there.
Also besides the error, why do you call the toString method in the constructor? It doesn't do anything.

Edited 6 Years Ago by javaAddict: n/a

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