2.1 Using Scanner Class
Scanner class එක use කරන්න නම්, මුලින්ම ඒක import කරගන්න ඕනෙ. ඊට පස්සෙ class එකේ නමින් object එකක් හදලා අපිට inputs ගන්න පුලුවන්.
Java:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//create Scanner object to read user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
//read a String input from the console
String name = sc.nextLine();
System.out.print("Enter your age: ");
//read an int input from the console
int age = sc.nextInt();
System.out.print("Your name is " + name + " and you are " + age + " years old.");
}
}
String inputs ගන්න nextLine(); සහ next(); කියන දෙකම use කරන්න පුළුවන්. හැබැයි වෙනසක් තියෙනවා. next(); දුන්නොත් ඔයා input කරන පලවෙනි වචනෙ විතරයි read කරන්නෙ. උදාහරණයක් විදියට ඔයා John Doe කියලා input කරොත් read වෙන්නෙ John විතරයි. nextLine(); දුන්නම එහෙම නෑ John Doe කියලම read කරනවා.
ඔයාට int එකක් input කරන්න ඕනෙ නම් nextInt() double නම් nextDouble() වගේ අනිත් data types වලටත් use කරන්න පුළුවන්.
අනිත් ඒවත් ටික ටික දාන්නම්.. කම්මැලියි අනේ type කරන්න.

මේක ඉතින් basic idea එක. ප්රශ්න එහෙම තිබුනොත් අහන්න. දන්න දෙයක් නම් කියන්නම්.