Basic Java Language: Difference between revisions

wikademia>MetaBohemian
→Comments: corrected javadoc comment
wikademia>Dmclean
Line 324: Line 324:
=Asking the User=
=Asking the User=
Now, let's go on to asking the user to enter a number and doing calculations on it. Basically: <code>JOptionPane.showInputDialog(null, "Hello. Enter something")</code>. Try putting that in a class (in the main method, of course) and run it. Currently, we can't do anything with it because the value is deleted.
Now, let's go on to asking the user to enter a number and doing calculations on it. Basically: <code>JOptionPane.showInputDialog(null, "Hello. Enter something")</code>. Try putting that in a class (in the main method, of course) and run it. Currently, we can't do anything with it because the value is deleted.
'''Note: To use JOptionPane, the class must '''import javax.swing;'''


Because what you enter can consist of letters OR numbers, and any length of them, what is "produced" by that is a String. The following will work: <code>String mine = JOptionPane.showInputDialog(null, "Now I can set a String!");</code>. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: <code>System.out.println(mine);</code>.
Because what you enter can consist of letters OR numbers, and any length of them, what is "produced" by that is a String. The following will work: <code>String mine = JOptionPane.showInputDialog(null, "Now I can set a String!");</code>. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: <code>System.out.println(mine);</code>.