Background:
Hey guys I'm making the first part of a greater program. We are instructed to create a program that stores a list of all our CD's. It has to be created in 3 different classes... that obviously link together.

My Question
How do I create an array that is filled with 10 songs that the user inputs? (under a method called addSongs)

- I've tried using import java.util.Scanner; but I don't believe a .nextString(); exists... I've used .nextInt(); before but not .nextString();

public class CD {

    public CD ()
    {
  String[] redAlbum;              // declares an array of strings

           redAlbum = new String[10];      // sets memory to 10 strings
            
          redAlbum[0] = /// something blank, " " maybe? 
          redAlbum[1] = /// something blank, " " maybe? 
          redAlbum[2] = /// something blank, " " maybe? 
          redAlbum[3] = /// something blank, " " maybe? 
          redAlbum[4] = /// something blank, " " maybe? 
          redAlbum[5] = /// something blank, " " maybe? 
          redAlbum[6] = /// something blank, " " maybe? 
          redAlbum[7] = /// something blank, " " maybe? 
          redAlbum[8] = /// something blank, " " maybe? 
          redAlbum[9] = /// something blank, " " maybe? 
    }
    
    public static void addSongs (String [] redAlbum)
    { 
/// SOMETHING GOES HERE!!! I Need to input songs into the array redAlbum.  How do I go about that?
    }

Recommended Answers

All 2 Replies

well first off you need to initialize a scanner using

Scanner song = new Scanner(System.in);

and then you need to call in the .nextline() method to grab all of the information the user inputs until they hit enter.

TJ

Don't know if this is what you'r exactly after but hope it helps :)

import java.util.Scanner;
public class CD{
	   
String[] redAlbum;              // declares an array of strings

          redAlbum = new String[11];      // sets memory to 10 strings
          
          addSongs(redAlbum);
           
         System.out.println("\nTrack 1 = " + redAlbum[1]);
         System.out.println("Track 2 = " + redAlbum[2]);
         System.out.println("Track 3 = " + redAlbum[3]);
         System.out.println("Track 4 = " + redAlbum[4]);
         System.out.println("Track 5 = " + redAlbum[5]);
         System.out.println("Track 6 = " + redAlbum[6]);
         System.out.println("Track 7 = " + redAlbum[7]);
         System.out.println("Track 8 = " + redAlbum[8]);
         System.out.println("Track 9 = " + redAlbum[9]);
         System.out.println("Track 10 = " + redAlbum[10]);

   }
	public static void addSongs (String  redAlbum[]){
    	/// SOMETHING GOES HERE!!! I Need to input songs into the array redAlbum.  How do I go about that?
		Scanner Input = new Scanner(System.in);
		for(int i = 1 ; i< redAlbum.length ; i++){
		System.out.println("Enter the song " + i + ": ");
		redAlbum[i] = Input.nextLine();
		}
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.