Array of Strings???

Thread Solved

Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Array of Strings???

 
0
  #1
Feb 21st, 2008
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,

  1. import java.io.*;
  2. public class Problem2
  3. {
  4.  
  5.  
  6. private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
  7.  
  8. public static void main(String[] arguments) throws IOException
  9. {
  10. System.out.println("Welcome. How many names do you wish to enter?");
  11. String input = stdin.readLine();
  12. int nameCount = Integer.parseInt(input);
  13.  
  14. for(int i = 0; i < nameCount; i++)
  15. {
  16. System.out.print("Enter name: ");
  17.  
  18. //NEED HELP HERE
  19.  
  20.  
  21. }
  22.  
  23. }
  24. }

Thanks for any help you can provide!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,445
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Array of Strings???

 
0
  #2
Feb 21st, 2008
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);
    String[] names = new String[nameCount];
    for(int i = 0; i < nameCount; i++)
      {
	System.out.print("Enter name: ");

	//NEED HELP HERE
        names[i] = stdin.readLine();
      }
  
  }
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 97
Reputation: plgriffith is an unknown quantity at this point 
Solved Threads: 6
plgriffith plgriffith is offline Offline
Junior Poster in Training

Re: Array of Strings???

 
0
  #3
Feb 21st, 2008
thanks ezzaral, that worked like a charm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,445
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Array of Strings???

 
0
  #4
Feb 21st, 2008
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.
  1. public static void main(String args[]) {
  2. try {
  3. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  4. System.out.println("Welcome. How many names do you wish to enter?");
  5. String input = stdin.readLine();
  6. int nameCount = Integer.parseInt(input);
  7. String[] names = new String[nameCount];
  8. for(int i = 0; i<nameCount; i++) {
  9. System.out.print("Enter name: ");
  10.  
  11. //NEED HELP HERE
  12. names[i] = stdin.readLine();
  13. }
  14. } catch(IOException ioe) {
  15. ioe.printStackTrace();
  16. }
  17. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC