java读取控制台输入以及next() ,nextLine(), nextInt() 和nextDouble()的比较

(1)读取控制台输入

java的控制台输入由System.in完成。
为了获得一个绑定到控制台的字符流,你可以把System.in包装在一个BufferedReader对象中创建一个字符流。

BufferedReader br = new BufferedReader(new 
InputStreamReader(System.in));

实例化BufferedReader类时需要传入一个IO对象。

BufferedReader对象创建后,我们便可以使用如下方法:

  • read()方法从控制台读取一个字符
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int charASCII = br.read();//这里读取的是输入的字符的ASCII码值
char inputChar = (char) charASCII; //强制类型转换为字符类型
System.out.println(inputChar+":"+charASCII);//打印出输入的字符和其ASCII码值

因为调用了read()方法,所以要抛出IOException,即在包含上述代码的方法外部要throws IOException,即声明IO异常;

  • readLine()方法读取一个字符串
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine(); //读取一个从控制台输入的字符串
System.out.println(inputString);

这里也是如此,方法外部也需要声明IO异常(throws IOException);

(2)从控制台读取多字符输入

public static void IOTest() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int ascii;
        char c;
        // 定义一个StringBuilder对象用来动态存储输入的字符
        StringBuilder str = new StringBuilder();
        System.out.println("输入字符,直到按下空格结束:");
        while (true){
            ascii = br.read(); //这里读取的是输入的字符的ASCII码值,即int类型的值
            // 当读取到输入的字符为换行符的时候跳出循环
            if (ascii == 10) //换行符的ASCII码值为10
                break;
            c = (char)ascii;
            str.append(c);
        }
        System.out.println(str);
}

运行结果如下:

输入字符,直到按下空格结束:
Believe yourself!
Believe yourself!

(3)从控制台读取多字符串

public static void IOTest() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        StringBuilder res = new StringBuilder();//用来存储输入的字符行

        System.out.println("请输入字符行,直到输入‘end’结束输入:");
        String str;
        while (true){
            str = br.readLine();
            if (str.equals("end")) //当输入end的时候,跳出循环
                break;
            res.append(str);
        }
        System.out.println(res.toString());
}

运行结果如下:

请输入字符行,直到输入‘end’结束输入:
You can fail,
but you can't lose your confidence!
end
You can fail,but you can't lose your confidence!

(4)通过Scanner读取控制台输入

我们可以通过Scanner类获取用户的输入

Scanner scanner = new Scanner(System.in);

通过Scanner类的next()nextLine()方法获取输入的字符串,在读取前一般需要使用hasNext()hasNextLine()判断是否还有输入的数据

next()

读取键盘输入的字符串并打印,直到用户输入字符串“end”为止。

public static void scannerTest(){
        Scanner scanner = new Scanner(System.in);//接收键盘上输入的数据
        System.out.println("next方式读取数据:");
        while (scanner.hasNext()){
            //当读取到输入的字符串是“end”,表示输入结束。
            String str = scanner.next();
            if (str.equals("end"))
                break;
            System.out.println("输入的数据为:"+str); // (1)
        }
        scanner.close();//关闭scanner对象
}

运行结果为:
(1)

next方式读取数据:
Hello, how are you ! end
输入的数据为:Hello,
输入的数据为:how
输入的数据为:are
输入的数据为:you
输入的数据为:!

Process finished with exit code 0

(2)

next方式读取数据:
Hello, How are you !
输入的数据为:Hello,
输入的数据为:How
输入的数据为:are
输入的数据为:you
输入的数据为:!
end

Process finished with exit code 0

将(1)处的输出语句改为如下形式,则:

System.out.print(str+" ");

其运行结果如下:

next方式读取数据:
Hello, how are you ! end
Hello, how are you! 

Process finished with exit code 0
  • (1)next()方法默认将空格作为字符串的分隔符,所以不能读取带有空格的字符串。

例如:h ello,这里是分为两个字符串,所以是读取两次,h 一次,ello 一次;

  • (2)next()方法可以修改默认的字符串分隔符;
// 上面对应的代码改成如下所示
Scanner scanner = new Scanner(System.in);
// 使用userDelimiter("_")将默认的字符串分隔符改为“_”(下划线)
String str = scanner.useDelimiter("_").next();

则运行结果为:

next方式读取数据:
Hello_How_are_you_!_end
Hello How are you ! 
  • (3)对输入有效字符之前的空白,next()方法会自动将其去掉;

nextLine()

nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions thecursor in the next line.

读取整行的数据包括单词间的空格,到回车结束(也就是从开始读一整行包括回车),读取结束后,光标放在下一行开头。

public static void scannerTest1(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("nextLine方式读取数据:");
        while (scanner.hasNextLine()){
            String str = scanner.nextLine();
            if (str.equals("end"))
                break;
            System.out.println("输出的数据为: "+str);
        }
        scanner.close();
}

运行结果:

nextLine方式读取数据:
Hello, How are you ! end
输出的数据为: Hello, How are you ! end    
Nice to meet you! end
输出的数据为: Nice to meet you! end
end

注意: 我们在输入“Hello,How are you ! end”后并没有结束输入,因为nextLine()读取的是输入回车键之前的所有字符,所以这里String str = scanner.nextLine()读取的是“Hello,How are you ! end”,即str = "Hello,How are you ! end",而不是str = "end",所以这里不会终此输入。
而下面的单独一行输入“end”之后, 才结束输入。

  • (1)以Enter(回车键)为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符;
  • (2)可以获取空白(读取空白);

nextInt()

nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.
只读取整数型数据, nextInt()在读取完输入后把光标放在读取数据的同一行,该数据的后面。

public static void scannerTest2(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("nextInt方式读取数据:");
        while (scanner.hasNextInt()){ //只要输入的数据不是整型数据,立马结束输入的读取
            int data = scanner.nextInt();
            System.out.println("输出的数据为: "+data);
        }
        scanner.close();
}
nextInt方式读取数据:
23 89 299 09 78 jksdf
输出的数据为: 23
输出的数据为: 89
输出的数据为: 299
输出的数据为: 9
输出的数据为: 78

Process finished with exit code 0

只要输入的不是整型数据(除分隔符以外),则结束输入。

nextDouble()

与nextInt()方法类似,不同的是只读取小数(double)类型的数据。

参考:

  1. Java 流(Stream)、文件(File)和IO
  2. java中的IO整理 – 整理得很详细(可以多读读)
  3. Java Scanner 类
  4. 小心:Scanner中关于next()、nextInt()和nextLine()的问题
  • 5
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值