Ok, I know C/C++ really well, but just started Java today and am having an issue. I have an assignment where I have to ask the user how many names he wants to enter, and then have him enter all of the names. After all of the names have been entered, they have to be printed back out. I am not worried about the printing part, but I am worried about how to store all of these names. So far I have,

import java.io.*; 
public class Problem2
{


  private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );

  public static void main(String[] arguments) throws IOException
  {
    System.out.println("Welcome. How many names do you wish to enter?");
    String input = stdin.readLine();
    int nameCount = Integer.parseInt(input);

    for(int i = 0; i < nameCount; i++)
      {
	System.out.print("Enter name: ");

	//NEED HELP HERE


      }
  
  }
}

Thanks for any help you can provide!

Yes, for your purposes a simple array of String would be all that was needed.

import java.io.*; 
public class Problem2
{


  private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );

  public static void main(String[] arguments) throws IOException
  {
    System.out.println("Welcome. How many names do you wish to enter?");
    String input = stdin.readLine();
    int nameCount = Integer.parseInt(input);
[B]    String[] names = new String[nameCount];[/B]
    for(int i = 0; i < nameCount; i++)
      {
	System.out.print("Enter name: ");

	//NEED HELP HERE
        [B]names[i] = stdin.readLine();[/B]
      }
  
  }
}

thanks ezzaral, that worked like a charm.

On a side note, never throw exceptions from main() - there is nothing above main in the call stack to handle them. Place try-catch blocks around the code inside main as needed and at the very least print a stack trace in the catch block.

public static void main(String args[]) {
        try {
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Welcome. How many names do you wish to enter?");
            String input = stdin.readLine();
            int nameCount = Integer.parseInt(input);
            String[] names = new String[nameCount];
            for(int i = 0; i<nameCount; i++) {
                System.out.print("Enter name: ");

                //NEED HELP HERE
                names[i] = stdin.readLine();
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
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.