As we discussed in previous post, that JOptionPane is one of many ways to take input from user in java. Scanner can also be used to take user input. It is a text scanner which can parse primitive types and strings using regular expressions.
Here is the code snippet to use Scanner to take input from user:
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("What is your name?"); String name = s.next(); System.out.println("Name: " + name); } } |
Output of the above code fragment would be:
What is your name?
David
Name: David
Hope you find the information presented here useful. Please leave your footprints in the comments section below for any queries or suggestions.
Amazing Website, Stick to the useful work. Regards.
I like the valuable information you supply for your
articles. I’llbookmark your weblog and take
a look at again right here regularly. I am fairly sure I’ll learn many new stuff right
right here! Good luck for the next!
My siite :: diabetes