Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collecting input using arrays

 
Robert Adams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic