java键盘输入的三种常见的方法总结

System.in.read

方法一:使用System.in.read()获取键盘输入,此方法只能一个字符一个字符的获取,获取到的变量类型为int,需要通过类型转换获取到自己期望的类型。该方法也会接收回车符(13)、换行符(10)。

package com.ips.io.keyboard;

import java.io.IOException;

public class Demo1 {

    public static void main(String[] args) {
        try {
            System.out.print("Enter a char :");
            while(true){
                char val = (char)System.in.read();
                if(val == 13 || val == 10){
//                  byte b = (byte)val;
//                  System.out.println(b);
                }else{
                    System.out.println("Your char is :" + val + "\r");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

BufferedReader

方法二:使用BufferedReader类的readLine方法从控制台接收一个字符串。

package com.ips.io.keyboard;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo2 {
    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while(true){
                System.out.print("Enter a String :");
                String val = bufferedReader.readLine();
                System.out.println("Your String is :" + val + "\r");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                bufferedReader = null;
            }
        }
    }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

Scanner

方法三:使用Scanner类实现键盘输入,而且该类提供了对应的类型转换方法,如:nextInt()、nextFloat()等,非常便于操作。其中nextInt()、nextFloat()方法不会接收回车符(13)、换行符(10),因此如果在nextInt()后增加nextLine()方法,对于nextInt()输入时的回车符(13)、换行符(10)会被nextLine()接收。对比代码1、代码2的执行结果,可以体会其中的区别。

代码1:

package com.ips.io.keyboard;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        Scanner sc = null;
        try {
             sc = new Scanner(System.in); 
             System.out.println("请输入您的姓名:"); 
             String name = sc.nextLine(); 
             System.out.println("请输入您的年龄:"); 
             int age = sc.nextInt(); 
             System.out.println("请输入您的体重(kg):"); 
             float salary = sc.nextFloat(); 
             System.out.println("您的个人信息如下:"); 
             System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"体重:"+salary); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sc != null){
                sc.close();
                sc = null;
            }
        }
    }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

代码2:

package com.ips.io.keyboard;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Scanner sc = null;
        try {
             sc = new Scanner(System.in); 
             System.out.println("请输入您的年龄:"); 
             int age = sc.nextInt(); 
             System.out.println("请输入您的姓名:"); 
             String name = sc.nextLine(); 
             System.out.println("请输入您的体重(kg):"); 
             float salary = sc.nextFloat(); 
             System.out.println("您的个人信息如下:"); 
             System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"体重:"+salary); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sc != null){
                sc.close();
                sc = null;
            }
        }
    }
}

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

最后总结一下next()和nextLine()的区别:
在java中,next()方法是不接收空格的,在接收到有效数据前,所有的空格或者tab键等输入被忽略,若有有效数据,则遇到这些键退出。nextLine()可以接收空格或者tab键,其输入应该以enter键结束。

             转载来源于: https://blog.csdn.net/claram/article/details/52057562 
             版权归作者所有    

System.in.read

方法一:使用System.in.read()获取键盘输入,此方法只能一个字符一个字符的获取,获取到的变量类型为int,需要通过类型转换获取到自己期望的类型。该方法也会接收回车符(13)、换行符(10)。

package com.ips.io.keyboard;

import java.io.IOException;

public class Demo1 {

    public static void main(String[] args) {
        try {
            System.out.print("Enter a char :");
            while(true){
                char val = (char)System.in.read();
                if(val == 13 || val == 10){
//                  byte b = (byte)val;
//                  System.out.println(b);
                }else{
                    System.out.println("Your char is :" + val + "\r");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

BufferedReader

方法二:使用BufferedReader类的readLine方法从控制台接收一个字符串。

package com.ips.io.keyboard;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo2 {
    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while(true){
                System.out.print("Enter a String :");
                String val = bufferedReader.readLine();
                System.out.println("Your String is :" + val + "\r");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                bufferedReader = null;
            }
        }
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

Scanner

方法三:使用Scanner类实现键盘输入,而且该类提供了对应的类型转换方法,如:nextInt()、nextFloat()等,非常便于操作。其中nextInt()、nextFloat()方法不会接收回车符(13)、换行符(10),因此如果在nextInt()后增加nextLine()方法,对于nextInt()输入时的回车符(13)、换行符(10)会被nextLine()接收。对比代码1、代码2的执行结果,可以体会其中的区别。

代码1:

package com.ips.io.keyboard;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        Scanner sc = null;
        try {
             sc = new Scanner(System.in); 
             System.out.println("请输入您的姓名:"); 
             String name = sc.nextLine(); 
             System.out.println("请输入您的年龄:"); 
             int age = sc.nextInt(); 
             System.out.println("请输入您的体重(kg):"); 
             float salary = sc.nextFloat(); 
             System.out.println("您的个人信息如下:"); 
             System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"体重:"+salary); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sc != null){
                sc.close();
                sc = null;
            }
        }
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

代码2:

package com.ips.io.keyboard;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Scanner sc = null;
        try {
             sc = new Scanner(System.in); 
             System.out.println("请输入您的年龄:"); 
             int age = sc.nextInt(); 
             System.out.println("请输入您的姓名:"); 
             String name = sc.nextLine(); 
             System.out.println("请输入您的体重(kg):"); 
             float salary = sc.nextFloat(); 
             System.out.println("您的个人信息如下:"); 
             System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"体重:"+salary); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sc != null){
                sc.close();
                sc = null;
            }
        }
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

最后总结一下next()和nextLine()的区别:
在java中,next()方法是不接收空格的,在接收到有效数据前,所有的空格或者tab键等输入被忽略,若有有效数据,则遇到这些键退出。nextLine()可以接收空格或者tab键,其输入应该以enter键结束。

             转载来源于: https://blog.csdn.net/claram/article/details/52057562 
             版权归作者所有    

猜你喜欢

转载自blog.csdn.net/qauchangqingwei/article/details/80888589