| | |
Array of Strings???
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 97
Reputation:
Solved Threads: 6
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,
Thanks for any help you can provide!
Java Syntax (Toggle Plain Text)
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);
String[] names = new String[nameCount];
for(int i = 0; i < nameCount; i++)
{
System.out.print("Enter name: ");
//NEED HELP HERE
names[i] = stdin.readLine();
}
}
} 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.
Java Syntax (Toggle Plain Text)
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(); } }
![]() |
Similar Threads
- Sorting a 2D Array of Strings (C++)
- how to read an array of strings in C++ (C++)
- 2d array of strings (C++)
- Declaring an array of strings in C++ (C++)
- Initializing an array of strings and printing it. (C++)
- Is there a way to tokenize an array of strings (C++)
- filling an array with strings using gets() (C)
Other Threads in the Java Forum
- Previous Thread: Issues with JOGL
- Next Thread: Regular GUI design help
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api applet application arc arguments array arrays automation banking binary bluetooth byte chat chatprogramusingobjects class client code color compare component count database design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode web windows






