aspose file tools*
The moose likes Beginning Java and the fly likes collecting input using arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "collecting input using arrays" Watch "collecting input using arrays" New topic
Author

collecting input using arrays

Robert Adams
Greenhorn

Joined: May 06, 2004
Posts: 2
What is the best way to gather input from a user and place it into an array? I can't use an applet, it must be an application. Is using a JOptionPane my only choice? When I do I can't figure out how to change the string info collected in the JOptionPane to an int, float, etc...
Any help would be appreciated.
Thanks,
Bob
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
My knowledge on J-things is a bit fuzzy, but one thing i DO know is how to turn strings into ints, doubles, etc. To turn String s into a int, use the static method parseInt from the wrapper class Integer:

good luck with input


I've heard it takes forever to grow a woman from the ground
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Originally posted by Robert Adams:
What is the best way to gather input from a user and place it into an array? I can't use an applet, it must be an application. Is using a JOptionPane my only choice? When I do I can't figure out how to change the string info collected in the JOptionPane to an int, float, etc...
Any help would be appreciated.
Thanks,
Bob

No, JOptionPane is not your only choice. One of the other options is to use a BufferedReader. For instance:


Here, you're creating a new input stream (using standard input), and passing this stream to the buffered reader. If you're familiar with C++, this works similar to cin. Once you've created your BufferedReader, you can use the readLine() method to get a line of input from the user.
So, for instance:

This code will repeatedly accept input from the user until the user types "quit". Once you get the input string, you can process it however you want.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that the line of input that Jeffrey is talking about would come from the system console.
So, if you're writing an application, to get user input, three common strategies are through the system console, with a JOptionPane, and through a text field (or text area) in a Frame (or JFrame).


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
Don't get me started about those stupid light bulbs.
 
subject: collecting input using arrays