在Java中,有多少种方法可以读取键盘输入的数据?


**java.io**包提供了各种类,用于从各种来源读取和写入数据到各种目标。

您可以使用各种类(例如,Scanner、BufferedReader、InputStreamReader、Console等)从用户(键盘)读取数据。

使用Scanner类

从Java 1.5开始引入Scanner类。此类接受File、InputStream、Path和String对象,使用正则表达式逐个标记读取所有基本数据类型和字符串(来自给定来源)。默认情况下,空格被视为分隔符(用于将数据分成标记)。

要从键盘读取数据,您需要使用标准输入作为来源(System.in)。对于每种数据类型,都提供了nextXXX()方法,例如nextInt()、nextShort()、nextFloat()、nextLong()、nextBigDecimal()、nextBigInteger()、nextLong()、nextShort()、nextDouble()、nextByte()、nextFloat()、next()。

示例

下面的Java程序使用Scanner类从用户读取数据。

import java.util.Scanner;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) {
      Scanner sc =new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.next();
      System.out.println("Enter your age: ");
      int age = sc.nextInt();
      System.out.println("Enter your percent: ");
      float percent = sc.nextFloat();
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = sc.nextBoolean();
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = sc.next().toCharArray()[0];
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

输出

Enter your name:
Krishna
Enter your age:
25
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 25
Percent: 86.0
Nationality: Indian
Grade: A

使用BufferedReader

Java的BufferedReader类用于从指定来源(字符输入流)读取字符流。此类的构造函数接受InputStream对象作为参数,您可以传递一个**InputStreamReader**。

示例

下面的Java程序使用**BufferedReader**类从用户读取数据。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) throws IOException {
      BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter your name: ");
      String name = reader.readLine();
      System.out.println("Enter your age: ");
      int age = Integer.parseInt(reader.readLine());
      System.out.println("Enter your percent: ");
      float percent = Float.parseFloat(reader.readLine());
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = Boolean.parseBoolean(reader.readLine());
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = (char) reader.read();
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

输出

Enter your name:
Krishna
Enter your age:
25
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 25
Percent: 86.0
Nationality: Indian
Grade: A

使用Console类

此类用于与控制台(键盘/屏幕)设备写入/读取数据。它提供了一个**readLine()**方法,该方法从键盘读取一行。您可以使用**console()**方法获取Console类的对象。

**注意** - 如果您尝试在非交互式环境(如IDE)中执行此程序,它将无法工作。

示例

下面的Java程序使用**Console**类从用户读取数据。

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
class Student{
   String name;
   int age;
   float percent;
   boolean isLocal;
   char grade;
   Student(String name, int age, float percent, boolean isLocal, char grade){
      this.name = name;
      this.age = age;
      this.percent = percent;
      this.isLocal = isLocal;
      this.grade = grade;
   }
   public void displayDetails(){
      System.out.println("Details..............");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Percent: "+this.percent);
      if(this.isLocal) {
         System.out.println("Nationality: Indian");
      }else {
         System.out.println("Nationality: Foreigner");
      }
      System.out.println("Grade: "+this.grade);
   }
}
public class ReadData {
   public static void main(String args[]) throws IOException {
      Console console = System.console();
      if (console == null) {
         System.out.println("Console is not supported");
         System.exit(1);
      }
      System.out.println("Enter your name: ");
      String name = console.readLine();
      System.out.println("Enter your age: ");
      int age = Integer.parseInt(console.readLine());
      System.out.println("Enter your percent: ");
      float percent = Float.parseFloat(console.readLine());
      System.out.println("Are you local (enter true or false): ");
      boolean isLocal = Boolean.parseBoolean(console.readLine());
      System.out.println("Enter your grade(enter A, or, B or, C or, D): ");
      char grade = console.readLine().toCharArray()[0];
      Student std = new Student(name, age, percent, isLocal, grade);
      std.displayDetails();
   }
}

输出

Enter your name:
Krishna
Enter your age:
26
Enter your percent:
86
Are you local (enter true or false):
true
Enter your grade(enter A, or, B or, C or, D):
A
Details..............
Name: Krishna
Age: 26
Percent: 86.0
Nationality: Indian
Grade: A

更新于:2019年8月1日

4K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.