Basic Java Language: Difference between revisions

wikademia>Lurkingfate
→System.out: Cleaned up code examples
wikademia>Lurkingfate
→Asking the user: Cleaned up code examples.
Line 355: Line 355:


The first way to get user input is from the keyboard without using the swing gui. You must first import the Scanner package first like so. <code>import java.util.Scanner;</code> Once you have done that you will have to create a Scanner object. <code>Scanner scan1 = new Scanner(System.in);</code> Finally after the user has entered the input asked for you retrieve using your Scanner object. For example if we wanted a string as input we could use the following code.  
The first way to get user input is from the keyboard without using the swing gui. You must first import the Scanner package first like so. <code>import java.util.Scanner;</code> Once you have done that you will have to create a Scanner object. <code>Scanner scan1 = new Scanner(System.in);</code> Finally after the user has entered the input asked for you retrieve using your Scanner object. For example if we wanted a string as input we could use the following code.  
<pre>
<source lang="java">
import java.util.Scanner;
import java.util.Scanner;
Public class GettingStringInput
Public class GettingStringInput
Line 364: Line 364:
       Scanner scan1 = new Scanner(System.in);
       Scanner scan1 = new Scanner(System.in);
       System.out.print("Please enter a string: ");
       System.out.print("Please enter a string: ");
       yourinput = scan1.nextLine()
       yourinput = scan1.nextLine();
   }
   }
}
}
</pre>
</source>


''In order for the following to work properly, you must place the following code on the first line of your .java file. Importing packages will be explained later.''
''In order for the following to work properly, you must place the following code on the first line of your .java file. Importing packages will be explained later.''
Line 373: Line 373:
<code>import javax.swing.JOptionPane;</code>
<code>import javax.swing.JOptionPane;</code>


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: <source lang="java">JOptionPane.showInputDialog( null, "Hello, enter something" );</source>. 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.


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: <source lang="java">String mine = JOptionPane.showInputDialog( null, "Now I can set a String!" );</source>. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: <source lang="java">System.out.println( mine );</source>.


Now you probably want to be able to enter a number. Java provides a method of changing a '''String''' to an <code>int</code>. It is <code>Integer.parseInt( String_Here );</code>. So lets say you want the user to enter a number and you print it. Do:
Now you probably want to be able to enter a number. Java provides a method of changing a '''String''' to an <code>int</code>. It is <source lang="java">Integer.parseInt( String_Here );</source>. So lets say you want the user to enter a number and you print it. Do:
<pre>
<source lang="java">
int mine = Integer.parseInt( JOptionPane.showInputDialog( null, "Now I can set an int!" ) );
int mine = Integer.parseInt( JOptionPane.showInputDialog( null, "Now I can set an int!" ) );
System.out.println( mine );
System.out.println( mine );
</pre>
</source>


And now, of course, you can do whatever you want with that...
And now, of course, you can do whatever you want with that...