อินพุตมาตรฐาน (stdin ) สามารถแสดงโดย System.in ในชวา System.in เป็นตัวอย่างของ InputStream ระดับ. หมายความว่าเมธอดทั้งหมดทำงานบนไบต์ ไม่ใช่สตริง หากต้องการอ่านข้อมูลใดๆ จากแป้นพิมพ์ เราสามารถใช้ คลาส Reader หรือ สแกนเนอร์ ชั้นเรียน
ตัวอย่าง1
import java.io.*;
public class ReadDataFromInput {
public static void main (String[] args) {
int firstNum, secondNum, result;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a first number:");
firstNum = Integer.parseInt(br.readLine());
System.out.println("Enter a second number:");
secondNum = Integer.parseInt(br.readLine());
result = firstNum * secondNum;
System.out.println("The Result is: " + result);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
} ผลลัพธ์
Enter a first number: 15 Enter a second number: 20 The Result is: 300
ตัวอย่าง2
import java.util.*;
public class ReadDataFromScanner {
public static void main (String[] args) {
int firstNum, secondNum, result;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a first number:");
firstNum = Integer.parseInt(scanner.nextLine());
System.out.println("Enter a second number:");
secondNum = Integer.parseInt(scanner.nextLine());
result = firstNum * secondNum;
System.out.println("The Result is: " + result);
}
} ผลลัพธ์
Enter a first number: 20 Enter a second number: 25 The Result is: 500